使用Sencha Touch的移动应用程序-JSON请求生成语法错误 [英] Mobile Application Using Sencha Touch - JSON Request Generates Syntax Error
问题描述
我开始使用Sencha Touch玩一些游戏.
I started playing a bit with Sencha Touch.
因此,我基于其中一个示例构建了一个非常简单的应用程序,以了解其运行方式.
So I've built a really simple application based on one of the examples just to see how it goes.
基本上,它创建一个JSON请求,该请求执行Last.FM Web服务以获取用户位置附近的音乐事件.
Basically it creates a JSON Request which executes a Last.FM web service to get music events near the user's location.
这是JSON代码:
var makeJSONPRequest = function() {
Ext.util.JSONP.request({
url: 'http://ws.audioscrobbler.com/2.0/',
params: {
method: 'geo.getEvents',
location: 'São+Paulo+-+SP',
format: 'json',
callback: 'callback',
api_key: 'b25b959554ed76058ac220b7b2e0a026'
},
callback: function(result) {
var events = result.data.events;
if (events) {
var html = tpl.applyTemplate(events);
Ext.getCmp('content').update(html);
}
else {
alert('There was an error retrieving the events.');
}
Ext.getCmp('status').setTitle('Events in Sao Paulo, SP');
}
})
};
但是每次我尝试运行它时,都会出现以下异常:
But every time I try to run it, I get the following exception:
未捕获到的SyntaxError:意外令牌:
Uncaught SyntaxError: Unexpected token :
有人知道吗?
推荐答案
几件事.首先,"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.
这篇关于使用Sencha Touch的移动应用程序-JSON请求生成语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!