web services - Mobile Application Using Sencha Touch - JSON Request Generates Syntax Error -
i started playing bit sencha touch.
so i've built simple application based on 1 of examples see how goes.
basically creates json request executes last.fm web service music events near user's location.
here's 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 error retrieving events.'); } ext.getcmp('status').settitle('events in sao paulo, sp'); } }) };
but every time try run it, following exception:
uncaught syntaxerror: unexpected token :
anyone has clue?
a couple of things. first of "uncaught syntaxerror: unexpected token :"
means browser javascript engine complaining colon ":"
has been put in wrong place.
the problem in returned json. since whatever server returns run though eval("{json http result}")
function in javascript, thing problem in there somewhere.
i've put code on little sencha test harness , found couple of problems it.
first: browser not happy "squiggly ã" in location: 'são+paulo+-+sp',
had change location: 'sao+paulo,+brazil',
worked , returned correct results audioscribbler api.
second: notice added callback: 'callback',
line request parameters, changes nature of http result , returns json follows:
callback({ // 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" } } }) // object gets wrapped parenthesis here
instead of doing think should using callbackkey: 'callback'
comes example in http://dev.sencha.com/deploy/touch/examples/ajax/index.js.
something 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 console (firebug/chrome/safari) console.log(result); // handle error logic if (result.error) { alert(result.error) return; } // continue code var events = result.data.events; // ... } });
that worked me it'll work too. cherio.
Comments
Post a Comment