[英]Uncaught SyntaxError: Unexpected token : on chrome and “SyntaxError: missing ; before statement” on firefox
My code is: 我的代码是:
function getCategoryResponse() {
var appid = "1";
$.ajax({
type: 'GET',
url: 'http://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/genres?id=36&callback=myCallbackFunction',
data: {},
dataType: 'jsonp',
success: function (data) {
var appData = data.results[0]; //get first record from array
console.log(appData);
return;
},
error: function () {
//alert('something bad happened');
console.log("something bad happened");
}
});
}
It results in: 结果是:
"Uncaught SyntaxError: Unexpected token :" /* on chrome */
"SyntaxError: missing ; before statement" /* on firefox */
That URL doesn't return a JSONP response, just a JSON response that starts like this: 该URL不会返回JSONP响应,而只会返回像这样开始的JSON响应:
{
"36": {
"name": "App Store",
"id": "36",
"url": "https://itunes.apple.com/us/genre/ios/id36?mt=8",
"rssUrls": {
"topAppsByRevenue": "https://itunes.apple.com/us/rss/topappsbyrevenue/genre=36/json",
"topPaidIpadApplications": "https://itunes.apple.com/us/rss/toppaidipadapplications/genre=36/json",
"topIpadAppsByRevenue": "https://itunes.apple.com/us/rss/topipadappsbyrevenue/genre=36/json",
"topFreeIpadApplications": "https://itunes.apple.com/us/rss/topfreeipadapplications/genre=36/json",
You'll have to consult the documentation to find out how to tell it you need JSONP, which would start like this: 您必须查阅文档以了解如何告诉您您需要JSONP,它的开始如下:
myCallbackFunction({
"36": {
"name": "App Store",
"id": "36",
"url": "https://itunes.apple.com/us/genre/ios/id36?mt=8",
"rssUrls": {
"topAppsByRevenue": "https://itunes.apple.com/us/rss/topappsbyrevenue/genre=36/json",
"topPaidIpadApplications": "https://itunes.apple.com/us/rss/toppaidipadapplications/genre=36/json",
"topIpadAppsByRevenue": "https://itunes.apple.com/us/rss/topipadappsbyrevenue/genre=36/json",
"topFreeIpadApplications": "https://itunes.apple.com/us/rss/topfreeipadapplications/genre=36/json",
Separately: When using jQuery, it's best to allow jQuery to specify the name of the callback function rather than specifying it yourself. 另外:使用jQuery时,最好允许jQuery指定回调函数的名称,而不是自己指定回调函数的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.