born to hula
born to hula

Reputation: 1286

Mobile Application Using Sencha Touch - JSON Request Generates Syntax Error

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.

Basically it creates a JSON Request which executes a Last.FM web service to get music events near the user's location.

Here's the JSON code:

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:

Uncaught SyntaxError: Unexpected token :

Anyone has a clue?

Upvotes: 2

Views: 13638

Answers (1)

Steven de Salas
Steven de Salas

Reputation: 21447

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.

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.

I've put your code on a little sencha test harness and found a couple of problems with it.

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.

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.

Something like this for example:

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

That worked for me so hopefully it'll work for you too. Cherio.

Upvotes: 2

Related Questions