Highlighted

'Unexpected token /' in response using SerializeJSON() with jQuery .ajax()

Community Beginner ,
Jan 03, 2016

Copy link to clipboard

Copied

Hello CF folk, I hope you can help.

I'm using jQuery .ajax to post a javascript array to a CF page, which queries a database table and returns a serialized result.  In the 'Network' tab of Developer Tools the result looks good and

contains expected data, however it gives an error message "Syntax error :Unexpected token: /".   The response is prefixed by two slashes as below.

//{"COLUMNS":["Field1","Field2","Field3","Field4"],"DATA":[[1,0,162,"text1"],[2,0,162,"text2"],[3,0,704,"text3"],[4,0,705,"text4"],[5,0,127,"text5"],[6,0,0,""]]}

The .ajax request is below:

  $.ajax({

       type: "POST",

       dataType: "json",

       url:  "/links.cfm",

       data: {"images" : JSON.stringify(thumbnails_array)},

       error: function(xhr, textStatus, errorThrown){

            alert("An error occurred: " + errorThrown);

       },

       success: function( data ) {

            alert("Reached success stage");

            $.each(data, function(key) {

            console.log(data[key][1]);

            if (data[key][1] !== 0) {

                 ---use this element---;

            }

            });

         }

  });

I'm assuming this // prefix is the unexpected token, how do I remove/resolve it?  In the called page 'links.cfm' I'm using SerializeJSON() on the query result to create the response.

Any help much appreciated,

Many thanks,

Views

935

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

'Unexpected token /' in response using SerializeJSON() with jQuery .ajax()

Community Beginner ,
Jan 03, 2016

Copy link to clipboard

Copied

Hello CF folk, I hope you can help.

I'm using jQuery .ajax to post a javascript array to a CF page, which queries a database table and returns a serialized result.  In the 'Network' tab of Developer Tools the result looks good and

contains expected data, however it gives an error message "Syntax error :Unexpected token: /".   The response is prefixed by two slashes as below.

//{"COLUMNS":["Field1","Field2","Field3","Field4"],"DATA":[[1,0,162,"text1"],[2,0,162,"text2"],[3,0,704,"text3"],[4,0,705,"text4"],[5,0,127,"text5"],[6,0,0,""]]}

The .ajax request is below:

  $.ajax({

       type: "POST",

       dataType: "json",

       url:  "/links.cfm",

       data: {"images" : JSON.stringify(thumbnails_array)},

       error: function(xhr, textStatus, errorThrown){

            alert("An error occurred: " + errorThrown);

       },

       success: function( data ) {

            alert("Reached success stage");

            $.each(data, function(key) {

            console.log(data[key][1]);

            if (data[key][1] !== 0) {

                 ---use this element---;

            }

            });

         }

  });

I'm assuming this // prefix is the unexpected token, how do I remove/resolve it?  In the called page 'links.cfm' I'm using SerializeJSON() on the query result to create the response.

Any help much appreciated,

Many thanks,

Views

936

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 03, 2016 0
Most Valuable Participant ,
Jan 03, 2016

Copy link to clipboard

Copied

This is a cross-site scripting (XSS) prevention security feature of ColdFusion. You have two choices: turn this feature off, or revise your JavaScript code to remove the "//" prefix before interpreting the JSON data.

To turn the feature off, log into ColdFusion Administrator and go to the Server Settings heading on the left navigation panel.  Select "Server".  On the page that opens, look for "Prefix serialized JSON with".  Notice that you have "//" (two forward slashes) in the box to the right of that heading.  Simply uncheck the checkbox on the left of the heading and click the "Submit Changes" button at the top of the page.  Alternatively, you can change the "//" value to something else you choose.  If you do change the value, you still have to deal with your JavaScript code.

If you choose to leave the prefix in place, you will just have to add some JavaScript code in the AJAX request callback to remove the prefix before parsing the JSON (between the alert and the $.each lines).

-Carl V.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 03, 2016 0
cf_rog LATEST
Community Beginner ,
Jan 04, 2016

Copy link to clipboard

Copied

Hi Carl - that's great, thanks so much for the info, I will try the removal route.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 04, 2016 0