更新时间:2022-12-21 20:57:12
几件事.首先,"Uncaught SyntaxError: Unexpected token :"
表示浏览器JavaScript引擎抱怨放置在错误位置的冒号":"
.
A couple of things. First of all the "Uncaught SyntaxError: Unexpected token :"
means the browser javascript engine is complaining about a colon ":"
that has been put in the wrong place.
问题很可能出在返回的JSON中.由于服务器返回的所有内容都将通过javascript中的eval("{JSON HTTP RESULT}")
函数运行,因此最有可能的问题是您的问题在某个地方.
The problem will most likely be in the returned JSON. Since whatever the server returns will be run though the eval("{JSON HTTP RESULT}")
function in javascript, the most likely thing is that your problem is in there somewhere.
我已经将您的代码放在了一个sencha测试工具上,并发现了一些问题.
I've put your code on a little sencha test harness and found a couple of problems with it.
首先:我的浏览器对location: 'São+Paulo+-+SP',
中的弯曲ã"不太满意,因此我不得不将其更改为location: 'Sao+Paulo,+Brazil',
,并且可以从audioscribbler API中返回正确的结果.
First: My browser was not too happy with the "squiggly ã" in location: 'São+Paulo+-+SP',
so I had to change this to location: 'Sao+Paulo,+Brazil',
which worked and returned the correct results from the audioscribbler API.
第二步::我注意到您在请求参数中添加了callback: 'callback',
行,该行更改了HTTP结果的性质并返回JSON,如下所示:
Second: I notice you added a callback: 'callback',
line to your request parameters, which changes the nature of the HTTP result and returns the JSON as follows:
callback({ // a function call "callback(" gets added here
"events":{
"event":[
{
"id":"1713341",
"title":"Skank",
"artists":{
"artist":"Skank",
"headliner":"Skank"
},
// blah blah more stuff
"@attr":{
"location":"Sao Paulo, Brazil",
"page":"1",
"totalpages":"1",
"total":"2"
}
}
}) // the object gets wrapped with extra parenthesis here
我认为您应该使用
Instead of doing that I think you should be using the callbackKey: 'callback'
that comes with the example in http://dev.sencha.com/deploy/touch/examples/ajax/index.js.
例如这样的东西:
Ext.util.JSONP.request({
url: 'http://ws.audioscrobbler.com/2.0/',
params: {
method: 'geo.getEvents',
location: 'Sao+Paulo,+Brazil',
format: 'json',
api_key: 'b25b959554ed76058ac220b7b2e0a026'
},
callbackKey: 'callback',
callback: function(result) {
// Output result to console (Firebug/Chrome/Safari)
console.log(result);
// Handle error logic
if (result.error) {
alert(result.error)
return;
}
// Continue your code
var events = result.data.events;
// ...
}
});
对我有用,所以希望对您也有用. Cherio.
That worked for me so hopefully it'll work for you too. Cherio.