2

我开始玩 Sencha Touch。

因此,我基于其中一个示例构建了一个非常简单的应用程序,只是为了看看它是如何运行的。

基本上,它创建一个 JSON 请求,该请求执行 Last.FM Web 服务以获取用户位置附近的音乐事件。

这是 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');
            }
        })
    };

但是每次我尝试运行它时,都会出现以下异常:

Uncaught SyntaxError: Unexpected token :

有人有线索吗?

4

1 回答 1

2

有几件事。首先,"Uncaught SyntaxError: Unexpected token :"浏览器 javascript 引擎抱怨冒号":"放错了位置。

问题很可能出在返回的 JSON 中。由于服务器返回的任何内容都将通过eval("{JSON HTTP RESULT}")javascript 中的函数运行,因此最有可能的是您的问题出在某个地方。

我已将您的代码放在一个小 sencha 测试工具上,并发现了一些问题。

首先:我的浏览器对“squiggly ã”不太满意,location: 'São+Paulo+-+SP',所以我不得不将其更改为location: 'Sao+Paulo,+Brazil',有效的,并从 audioscribbbler API 返回正确的结果。

第二:我注意到您callback: 'callback',在请求参数中添加了一行,这改变了 HTTP 结果的性质并返回 JSON,如下所示:

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

而不是这样做,我认为您应该使用http://dev.sencha.com/deploy/touch/examples/ajax/index.jscallbackKey: 'callback'中的示例附带的。

例如这样的事情:

   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;
                // ...
            }
        });

这对我有用,所以希望它也对你有用。切里奥。

于 2010-11-10T21:41:05.293 回答