我試圖做一個跨域JSONP呼叫在Chrome,但我不斷收到回「未捕獲的SyntaxError:意外的標記:」 我已經試過:改變響應內容類型,設置XHR頭,JSON.stringify,只是大多數這裏提供上,但該方案沒有奏效,到目前爲止:-(jQuery的AJAX JSONP錯誤「意外令牌」
$.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'
}
在調試器中,我可以看到的響應
{"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。
這裏是響應頭
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
任何人有任何意見/建議嗎?
對於JSONP,你需要一個函數包裝器。普通的JSON對象不是有效的JSONP響應。服務器實際上是否能夠提供JSONP-Responses? – Christoph 2013-02-13 12:18:27
另外'dataType:'jsonp',jsonp:false'看起來不正確。 – Christoph 2013-02-13 12:27:55
@Christoph Re'dataType:'jsonp',jsonp:false'他們是正確的,查看文檔http://api.jquery.com/jQuery.ajax/ – Eric 2013-02-13 13:03:38