Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery 1.5 AJAX call fails with "invalid label" for JSON requests

I've just upgraded from version 1.4 to version 1.5 of jQuery, and now my AJAX calls always fail with the "invalid label" error.

An example request is:

jQuery.ajax({
    async: false
    , dataType: "json"
    , error: function (xhr, status, error) { ... }
    , success: function (data, status, xhr) { ... }
    , type: "post"
    , url: "ajax/request.asp"
});

On the net I found this error is raised when the returned JSON is not wrapped with jQuery's callback (e.g. jQuery1234({ "something": "abcd" }).

The problem is I'm returning a JSON, not a JSONP (and I state it in the AJAX request), so why I must specify a callback in the returned JSON?

The 1.5 changelog says nothing about this... (Or it's me who can't read?)

Update:

This is an example of a not working JSON:

{
   "esito":"Ok",
   "centriCosto":[
      {
         "id":"1",
         "descrizione":"Colazione"
      },
      {
         "id":"2",
         "descrizione":"Pranzo"
      },
      {
         "id":"3",
         "descrizione":"Cena"
      }
   ]
}

And this is the same callback-wrapped working JSON:

jQuery1502710949228847014_1296739130498({
   "esito":"Ok",
   "centriCosto":[
      {
         "id":"1",
         "descrizione":"Colazione"
      },
      {
         "id":"2",
         "descrizione":"Pranzo"
      },
      {
         "id":"3",
         "descrizione":"Cena"
      }
   ]
})

By the way, Firebug says both of them are valid JSONs (and he's very picky about correctness).

like image 618
Albireo Avatar asked Feb 03 '11 13:02

Albireo


2 Answers

Ok, I found out what the hell is happening.

jQuery's Validation plug-in is not compatible with jQuery 1.5 (see one and two), removing the plug-in yields to the right behaviour.

If someone else has this problem, there's a patch in the plug-in's repository: link

like image 159
Albireo Avatar answered Oct 10 '22 12:10

Albireo


I actually ran into similar problem but it appears to be related to this bug: http://bugs.jquery.com/ticket/8398

It is not necessarily related to jQuery-validate and it took me a while to figure things out. It turns out that jQuery 1.5 is modifying subsequent ajax calls for json to jsonp which leads to this error.

I fixed it by following one of the workarounds suggested in the bug change history and placing the following code somewhere before my ajax calls are made:

$.ajaxSetup({
   jsonp: null,
   jsonpCallback: null
});

Should fix any problems for other ajax requests too.

like image 38
Tom Avatar answered Oct 10 '22 10:10

Tom