Chrome returns "SyntaxError: Unexpected Token"

OK, this question was asked quite often and in many ways, but STILL I did not find an answer that would atone for me from this torture.

My code works fine with Safari, Internet Explorer v.8. However, Chrome displays the message "SyntaxError: Unexpected token" - only in certain combinations (this is about the chain).

Below is the JSON that I am returning from an ASP (classic) page. It works on 99% of the inputs, but for the following JSON text, it throws an error:

{"queryStatus":"Not_Empty", "printID":["50674","54648","50437","37564","37566","37565","49124","null"], "printName":["BBB-12312313","BBB-1558","BBB-2-065 + 2-066","BBB-2-217","BBB-2-217b","BBB-2-226/2-217b ","BBB-2-5961b","null"]} 

Now my JS code looks like this:

 $.ajax({ type: "GET", url: "callbacks/xmlHTTPRequestGetPrintItems.asp?LabelID=" + $("#LicenseLabel").val() + "&RequestType=0", data: "", contentType: "application/json; charset=utf-8", dataType: "json", async: false, success: function (data, textStatus, jqXHR) { if (data.queryStatus == "Not_Empty") { for (i = 0; i < data.printID.length - 1; i++) { $('#ApprovalItem').append($('<option>', { value: data.printID[i] }).text(data.printName[i])); } } }, error: function (xhr, ajaxOptions, thrownError) { alert(thrownError); } }); 

Result: "SyntaxError: Unexpected token"

Here is what I tried:

  • Changing the "contentType" / deleting it, both the $ .ajax option and the server-side response;
  • Search for special characters - nothing;
  • Stretching the "zero" lines at the end of the object;

I read about the zero-width width distance that I could extract from the database (perhaps Unicode-200B?), But so far I have not been able to resolve it.

AGAIN - it runs on Chrome on 99% of the inputs, and on IE 8 it runs 100%. Help would be greatly appreciated, cheers.

+4
source share
1 answer

This may or may not work. I cannot reproduce the problem with the above information, which obviously makes it difficult for you to help, but this will be my first suggestion ...

 $.ajax({ type: "GET", url: "callbacks/xmlHTTPRequestGetPrintItems.asp?LabelID=" + $("#LicenseLabel").val() + "&RequestType=0", data: "", contentType: "application/json; charset=utf-8", dataType: "text", async: false, success: function (data, textStatus, jqXHR) { data = JSON.parse(data); if (data.queryStatus == "Not_Empty") { for (i = 0; i < data.printID.length - 1; i++) { $('#ApprovalItem').append($('<option>', { value: data.printID[i] }).text(data.printName[i])); } } }, error: function (xhr, ajaxOptions, thrownError) { alert(thrownError); } }); 

All I did was to specify the data type as text, not JSON, and then use JSON.parse() when it returns. It will at least determine if the error is a challenge or an answer (but I think we already know that).

+2
source

Source: https://habr.com/ru/post/1501841/


All Articles