jQuery 1.5 AJAX call fails with “invalid label” fo

2019-02-06 13:24发布

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).

8条回答
仙女界的扛把子
2楼-- · 2019-02-06 13:32

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.

查看更多
小情绪 Triste *
3楼-- · 2019-02-06 13:33

Updating to jquery 1.7 solves it

查看更多
爷的心禁止访问
4楼-- · 2019-02-06 13:38

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

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-02-06 13:38

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.

查看更多
贪生不怕死
6楼-- · 2019-02-06 13:40

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

jQuery.ajaxPrefilter("json jsonp", function( s, originalSettings, dataIsString /* internal */ ) {

removing the "json" from first argument will add correct behavior when spec. dataType:"json"

查看更多
劳资没心,怎么记你
7楼-- · 2019-02-06 13:43

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.

查看更多
登录 后发表回答