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).
Here is a possible workaround for those with the validator plugin.
dataType: "text json"
Works like a charm. Don't ask me why. On chrome, you can see a jquery syntax error parsing the ":" on the json return. And make no mistake about it, the return is valid json. I didn't try it but I suspect Tom's answer above will also work.
Updating to jquery 1.7 solves it
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
If your server-side code is relying on the callback name beginning with "jsonp", you may have a problem. The callback name prefix was changed to "jQuery" in version 1.5.
try: Did a quick search for json in jquery-1.5.js and found this on row 6905:
// Detect, normalize options and install callbacks for jsonp requests
removing the
"json"
from first argument will add correct behavior when spec.dataType:"json"
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:
Should fix any problems for other ajax requests too.