JQuery AJAX JSONP "Unexpected Token" Error

I'm trying to make a JSONP cross domain in Chrome, but I keep returning "Untrained SyntaxError: Unexpected token:" I tried: changing the type of the response content, setting the headers xhr, JSON.stringify, almost all the solutions proposed here, but nothing worked yet: - (

$.ajax({ type: "POST", url: "https://www.virustotal.com/vtapi/v2/url/report", crossDomain: true, contentType: "application/json; charset=UTF-8", dataType: 'jsonp', data: { apikey: "*", resource: "http://www.1001freefonts.com/font/BaroqueScript.zip" }, jsonp: false, jsonpCallback: receive, success: function (data, textStatus, jqXHR) { console.log("Data retrieved: " + data); } }).done(function () { console.log('I think we are done here'); }) .error(function (e) { console.log(arguments); console.log('something went funny here'); }) .complete(function (xhr, status) { console.log("complete"); if (status === 'error' || !xhr.responseText) { console.log('error'); } else { console.log("data found:" + xhr.responseText); //... } }); }); function receive(saveData) { if (saveData == null) { console.log("DATA IS UNDEFINED!"); // displays every time } console.log("Success is " + saveData); // 'Success is undefined' } 

In the debugger, I see the answer

 {"permalink": "https://www.virustotal.com/url/b5b546fdbb49a2258e951c5e568a52655c65ac56112e39d15af0954a53b36772/analysis/1360339512/", "url": "http://www.1001freefonts.com/font/BaroqueScript.zip", "response_code": 1, "scan_date": "2013-02-08 16:05:12", "scan_id": "b5b546fdbb49a2258e951c5e568a52655c65ac56112e39d15af0954a53b36772-1360339512", "verbose_msg": "Scan finished, scan information embedded in this object", "filescan_id": "b7e13c0242e9690aba1f3da4b73d9c2e99a9b7fd03f542b55e694a34aaf9eca8-1360339519", "positives": 0, "total": 35, "scans": {"CLEAN MX": {"detected": false, "result": "clean site"}, "MalwarePatrol": {"detected": false, "result": "clean site"}, "ZDB Zeus": {"detected": false, "result": "clean site"}, "K7AntiVirus": {"detected": false, "result": "clean site"}, "Quttera": {"detected": false, "result": "clean site"}, "Yandex Safebrowsing": {"detected": false, "result": "clean site"}, "MalwareDomainList": {"detected": false, "result": "clean site"}, "ZeusTracker": {"detected": false, "result": "clean site"}, "zvelo": {"detected": false, "result": "clean site"}, "Google Safebrowsing": {"detected": false, "result": "clean site"}, "BitDefender": {"detected": false, "result": "clean site"}, "Opera": {"detected": false, "result": "clean site"}, "G-Data": {"detected": false, "result": "clean site"}, "C-SIRT": {"detected": false, "result": "clean site"}, "Sucuri SiteCheck": {"detected": false, "result": "clean site"}, "VX Vault": {"detected": false, "result": "clean site"}, "ADMINUSLabs": {"detected": false, "result": "clean site"}, "SCUMWARE.org": {"detected": false, "result": "clean site"}, "Dr.Web": {"detected": false, "result": "clean site"}, "AlienVault": {"detected": false, "result": "clean site"}, "Malc0de Database": {"detected": false, "result": "clean site"}, "SpyEyeTracker": {"detected": false, "result": "clean site"}, "Phishtank": {"detected": false, "result": "clean site"}, "Avira": {"detected": false, "result": "clean site"}, "Antiy-AVL": {"detected": false, "result": "clean site"}, "Comodo Site Inspector": {"detected": false, "result": "clean site"}, "Malekal": {"detected": false, "result": "clean site"}, "ESET": {"detected": false, "result": "clean site"}, "SecureBrain": {"detected": false, "result": "unrated site"}, "Netcraft": {"detected": false, "result": "clean site"}, "ParetoLogic": {"detected": false, "result": "clean site"}, "URLQuery": {"detected": false, "result": "unrated site"}, "Wepawet": {"detected": false, "result": "unrated site"}, "Minotaur": {"detected": false, "result": "clean site"}}} 

I tested it at http://jsonlint.com/ and it shows that it is valid JSON.

Here are the response headers

 cache-control:no-cache content-encoding:gzip content-length:695 content-type:application/json date:Wed, 13 Feb 2013 12:00:33 GMT server:Google Frontend status:200 OK vary:Accept-Encoding version:HTTP/1.1 

Anyone have any ideas / suggestions?

+4
source share
3 answers

After rigorous testing, it was obvious that it was not possible to get the results of a JSON object when ajax expects JSONP in return (due to cross-domain restrictions). Even if the response status = 200.

I tested the use of jQuery AJAX calls, trying to see if the result could still be captured - despite the fact that a Parsing error occurred in the browser, but this did not seem possible. It looks like the response text goes to the header after the JS shuts down.

As suggested by @Florian F. @Likwid_T @Christoph, the server side of the script is definitely needed to make it work properly. Other developers seem interested in using a proxy written in C # as a solution.

+3
source

JSONP does not work out of the box.

JSONP bypasses the restriction between domains by loading the result in a script tag.

Basically, your server should have JSONP enabled.

Do everything you need to do on the server side, and then before sending a response:

  • Check if request "_callback" is set
  • If set, wrap your content with _callback.
  • Submit your details

Sample code in PHP:

 $responseString = '{"smthing":"val","smthingelse":"val2"}'; if (isset($_REQUEST['_callback'])) { $responseString = $_REQUEST['_callback'] . '(' . $responseString . ');'; } 

It will execute your "made" anonymous function with the correct arguments. (JQuery handles everything else)

+1
source

Christophe is right, you need to enclose your answer in your callback function, in your case at the end of the PHP file:

 echo $_GET['receive'] . '(' . json_encode($yourResultObject) . ');'; 

or if you need a more complex object

 echo $_GET['receive'] . '(' . json_encode(array(name1 => object1, name2 => object2, name3 => object3)) . ');'; 

You may need to tweak it a bit, but basically, when I get an Unexpected Token , it is almost always a syntax error that causes jQuery not to receive my callback function.

+1
source

All Articles