Adyen Web SDK初始化异常:JSON格式错误排查求助
Let's troubleshoot this issue step by step—based on the error message and your test cases, the core problem lies in how the paymentSession is being handled before passing it to the SDK.
Why You're Seeing This Error
The error explicitly states typeof data: boolean, which means the SDK is receiving a boolean value instead of the expected valid JSON object for paymentSession. Even though you mentioned it's a long string from Postman, there's a high chance either:
- The raw string isn't being parsed into a JSON object before passing it to
chckt.checkout(), or - The string itself is invalid JSON (e.g., missing quotes, extra whitespace, or corrupted during copy-paste), causing the SDK's internal parsing to fail and fall back to an unexpected boolean value.
Step-by-Step Fixes
1. Explicitly Parse the paymentSession String
Adyen's Web SDK expects the paymentSession parameter to be a parsed JSON object, not a raw string. Here's how to adjust your code:
// First, confirm the type and content of your paymentSession console.log("Payment session type:", typeof paymentSession); console.log("Payment session content snippet:", paymentSession.slice(0, 50)); // Wrap parsing in try/catch to catch invalid JSON issues try { const parsedSession = JSON.parse(paymentSession); // Pass the parsed object to the SDK instead of the raw string var checkout = chckt.checkout(parsedSession, '#payment-container', sdkConfigObj); } catch (parseError) { console.error("Failed to parse paymentSession:", parseError); // If this throws, your string is invalid—check the Postman response again }
2. Validate the Raw paymentSession String
If the JSON.parse() call fails, double-check these details:
- Did you copy the full response from Postman? Partial copies (e.g., missing the opening
{or closing}) break JSON validity instantly. - Are there extra characters (leading/trailing spaces, newlines, or accidental escape sequences) that snuck in during copy-paste? Use a JSON validator tool to paste the string and confirm it's well-formed.
- Is the Postman response itself valid? Inspect the "Body" tab in Postman to ensure the API isn't returning malformed JSON.
3. Rule Out Accidental Variable Modification
Make sure no other code is altering the paymentSession variable before you pass it to the SDK. For example, logic like if (paymentSession) { ... } could implicitly convert the string to a boolean if the string is empty or incorrectly evaluated, leading to this exact error.
Additional Context
Your test results (getting specific errors for invalid inputs like numbers or non-existent DOM nodes) confirm the SDK is working as expected for bad inputs. The fact that you're hitting a JSON formatting error with your "correct" input means the issue is definitely tied to how paymentSession is structured or passed.
内容的提问来源于stack exchange,提问作者Radu Florin Dunărențu

