Wednesday, April 20, 2011

JSONP and invalid label

Using mootools and JsonP I get "invalid label" error in Firefox Error console

JsonP seems to work (I get the data correctly)

{"jsondata":[{"title":"title1","link":"http://xxxx.xxx.xxx","thumbsrc":"http://xxxx.xxx.xxx/17_t.jpg" ,"description":".......","pubDate":"2009-03-09 06:26:00",},{"title":"title2","link":"http://xxxx.xxx.xxx","thumbsrc":"http://xxxx.xxx.xxx/16_t.jpg" ,"description":".......","pubDate":"2009-03-09 06:08:09",}]}

but I get the Invalid label error on "jsondata"

the same file works good with request.json

From stackoverflow
  • This could be due to the extra commas after the dates

  • Putting it in here:

    http://json.parser.online.fr/

    Shows that its valid, but has the extra comma (which will bork IE, although FF should handle it). If removing the comma doesn't fix it, you'll need to post more of your code to help us find the error.

  • comma removed... nothing

    this is the code I'm using

    window.addEvent('domready', function() {

    var gallery = $('gallery'); 
    
    new JsonP('http://myjsoncodeurl',{
      onComplete: function(jsonObj) {
       addImages(jsonObj.jsondata);
      }
     }).request();
    
    var addImages = function(images) {
     images.each(function(image) {
      var el = new Element('div', {'class': 'item'});
      var name = new Element('h3').inject(el);
      var a1 = new Element('a', {'href': image.link,'html': image.title}).inject(name);      
      var desc = new Element('span', {'html': image.description}).inject(name, 'after');
      var a2 = new Element('a', {'href': image.link}).inject(desc,'after');    
      var img = new Element('img', {'src': image.thumbsrc}).inject(a2);
      el.inject(gallery);
     });
    };
    

    });

    it works with normal request.Json, but JSONP that doesn't like my code :(

  • the same file works good with request.json

    With JSONP, your response should be returning a JavaScript function call (i.e. callback) with the JSON data passed in as the argument. If your response is a plain old JSON text, it won't work in the context of JSONP. You have to tailor your backend to accept a callback argument and call that callback with the JSON data.

0 comments:

Post a Comment