jQuery 1.5 AJAX call fails with “invalid label” for JSON requests

↘锁芯ラ 提交于 2019-12-18 13:17:00

问题


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


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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"




回答5:


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:


looks like this is fixed now in v1.6 - had the same issue after upgrading to ver 1.5.1 & after upgrading to 1.6 the issue disappeared.




回答7:


Updating to jquery 1.7 solves it




回答8:


Here's the solution:

$.post("...", {},
        function(data) {

      // dont forget to add below lines         

         },"json"); 


来源:https://stackoverflow.com/questions/4886525/jquery-1-5-ajax-call-fails-with-invalid-label-for-json-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!