• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Jan 03, 2016 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

1.2K

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
community guidelines
Guide ,
Jan 03, 2016 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.

Votes

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
community guidelines
Community Beginner ,
Jan 04, 2016 Jan 04, 2016

Copy link to clipboard

Copied

LATEST

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

Votes

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
community guidelines
Resources
Documentation