jQuery AJAX JSONP错误“意外令牌” [英] jQuery AJAX JSONP error "Unexpected token"
问题描述
我正在尝试在Chrome中进行跨域 JSONP 调用,但我不断回复未捕获的SyntaxError:意外的令牌:
I尝试过:改变响应内容类型,设置xhr标题,JSON.stringify,这里提供的大部分解决方案,但到目前为止没有任何工作: - (
I'm trying to make a cross domain JSONP call in Chrome, but the I keep getting back "Uncaught SyntaxError: Unexpected token :" I've tried: changing the response content type, setting xhr headers, JSON.stringify, just about most of the solutions offered on here but nothing worked so far :-(
$.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 can see the response
{"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"}}}
我已在 http://jsonlint.com/ 它显示它是一个有效的JSON。
I've verified it on http://jsonlint.com/ and it shows that it's a valid JSON.
以下是响应标头
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?
推荐答案
经过全面测试后,显然无法捕获JSON对象的结果ajax期待JSONP的回报(由于跨域限制)。即使响应状态= 200。
After thorough testing, it was evident that it's not possible to capture the JSON object results when ajax is expecting JSONP in return (due to cross-domain restrictions). Even though the response status = 200.
我一直在测试使用jQuery AJAX调用,试图看看是否仍然可以捕获结果 - 尽管抛出了解析错误通过浏览器 - 但似乎没有可能。 JS工作完成后,看起来响应文本到达标题。
I've been testing using jQuery AJAX calls, in attempt to see if the result can still be captured - despite there's Parsing Error thrown by the browser - but it didn't seem that's possible. It looks like that the Response Text arrives in the header after JS work is completed.
正如@Florian F. @Likwid_T @Christoph上面提到的,服务器端脚本绝对需要让它正常工作。其他开发人员似乎热衷于使用C#编写的代理作为解决方案。
As @Florian F. @Likwid_T @Christoph have suggested above, a server-side script is definitely required to make it working properly. Other developers seem to be keen on using a proxy written in C# as a solution.
这篇关于jQuery AJAX JSONP错误“意外令牌”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!