jQuery中如何正确提取custCNFormData内的cdReference值?
cdReference from JSON String Hey there! The problem you're facing is super common—your creditNoteHeader variable is holding a JSON string, not a JavaScript object. Even though the console output looks like an object, it's actually just text formatted like JSON. That's why trying to access .cdReference directly returns undefined—strings don't have properties like that!
How to Fix It
You need to convert the JSON string into a proper JavaScript object using JSON.parse(). Here's your updated code:
var custCNFormData = custCNFormSubmit(); var creditNoteHeader = custCNFormData.get('creditNoteHeader'); // Convert the JSON string to a JS object var creditNoteHeaderObj = JSON.parse(creditNoteHeader); console.log('value:'+creditNoteHeader); console.log('cdReference is : '+creditNoteHeaderObj.cdReference); // Now this will output "3"!
Quick Check to Confirm
If you want to verify the type of creditNoteHeader before parsing, you can add this line:
console.log(typeof creditNoteHeader); // This will log "string" if it's a JSON string
Once you parse it into an object, you'll also be able to access nested properties like creditNoteHeaderObj.customer.customerId without issues.
内容的提问来源于stack exchange,提问作者Jacel

