0

I have stored json string in my db field like following which is a valid json:

{"contact_person_name":"abcd","address_line1":"line1","address_line2":"line2","postal_code":"1111","country_id":"2","state":"someState","city":"someCity"}

when I am trying to pass this json string variable requestData.fields_values in a function parameter it is being printed in console like following while clicking the anchor tag:

literal not terminated before end of script

my JavaScript function is following:

function performChangeRequest(vendorId, requestedData){
            console.log('data: ' + requestedData);
}

I am appending dynamically data like following:

<a href="javascript:performChangeRequest('${requestData.vendor_id}', '${requestData.fields_values}' )"></a>

If I print the first variable vendorId it is working fine.

Any help? I have tried looking around

5
  • When you save the object in the database, even though it is JSON, it is being retrieved as string Commented Nov 4, 2018 at 12:32
  • @hngr18 JSON is a string Commented Nov 4, 2018 at 12:33
  • The string contains these: " So the tag gets broken (the href attribute is delimited by these) <a href="javascript:performChangeRequest('${requestData.vendor_id}', '${requestData.fields_values}' )"></a> Do a string replace to change the JSON's " to \" (escaping the ") Commented Nov 4, 2018 at 12:34
  • Apologies, first time writing comments, wanted to do a carriage return and posted prematurely! Commented Nov 4, 2018 at 12:34
  • can you please write that for me as a parameter? I am getting a bit confused @hngr18 Commented Nov 4, 2018 at 12:36

1 Answer 1

1

When you retrieve the JSON from the database and set requestData.fields_values, instead of assigning

requestData.field_values = **data**

instead do

requestData.field_values = **data**.replace('"', '\\"')

If you don't have the ability to intercept this value as it is being retrieved from the database, try instead to change the anchor tag to something like this:

<a href="javascript:performChangeRequest('${requestData.vendor_id}', '${requestData.fields_values.replace('"', '\\"')}' )"></a>

In the function performChangeRequest do console.log('data: ' + JSON.parse(requestedData));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.