简体   繁体   English

jQuery Ajax响应未调用成功方法

[英]Jquery ajax response not calling Success method

I am pretty much new to ajax and working on jquery ajax request. 我对ajax非常陌生,并且正在处理jquery ajax请求。 Ajax callback is not calling success method. Ajax回调未调用成功方法。 Interaction is between cross-site domains. 交互是跨站点域之间的。 My AJAX request looks like 我的AJAX请求看起来像

   $.ajax({
       timeout: 20000,
       url: 'test.com',
       crossDomain: true,
       dataType: 'jsonp',
       success: function (data) {
           console.log('callback success');
           this._cache = data;
           localStorage.token = data.access_token;
     } });

There are no errors in this call. 此通话没有错误。

This ajax request is not calling success function.Request is returning json data. 这个ajax请求没有调用成功函数,请求正在返回json数据。 it's just success method is not getting called. 只是成功方法没有被调用。

This ajax request is not calling success function. 此ajax请求未调用成功函数。

Get request is getting fired successfully. 获取请求已成功触发。 I can even trace the response in fiddler with 200 http response.For some reason success method is not getting called. 我什至可以用200 http响应在提琴手中跟踪响应。由于某种原因,未调用成功方法。

it's returning json object, which I've traced in fiddler 它正在返回json对象,我在提琴手中已经找到了

You're telling jQuery to expect a JSONP response, so it is trying to execute the JSON document as if it were a JavaScript script (because that is what JSONP is). 您要让jQuery期待JSONP响应,因此它试图像执行JavaScript脚本一样执行JSON文档(因为这就是JSONP)。 This fails because it is not JSONP. 失败,因为它不是JSONP。

Either return JSONP instead of JSON or (assuming the server returns the correct Content-Type ) remove dataType: 'jsonp', . 返回JSONP而不是JSON,或者(假设服务器返回正确的Content-Type )remove dataType: 'jsonp',

ok... I came here with the same problem... and when I read that specifying datatype:jsonp never calls success as a callback per @mondjunge from a comment above, it started me thinking about some behavior I saw earlier from my code and that maybe datatype:json might have the same behavior for what ever reason here too. 好的...我来到这里遇到了同样的问题...当我从上面的注释中读取到指定datatype:jsonp永远不会将成功作为每个@mondjunge的回调调用时,它开始让我开始思考我之前在代码中看到的某些行为也许出于某种原因, datatype:json可能也具有相同的行为。

So after reading this page I took out my datatype declaration from my ajax request and my servlet returned the proper data payload, returned a 200, and jquery called the success function finally and modified my DOM. 因此,阅读完此页面后,我从ajax请求中取出了数据类型声明,并且servlet返回了正确的数据有效负载,返回了200,并且jquery最终调用了成功函数并修改了我的DOM。

All those steps happened except the last one until I removed my datatype from my ajax call. 所有这些步骤都发生了,除了最后一个步骤,直到我从ajax调用中删除了数据类型。 NOT what I was expecting! 不是我所期望的!

Hopefully someone else can shed some light on why this happens... for now at least the few that don't lose their minds to this issue that find this post can do this in the mean time. 希望其他人可以弄清楚为什么会发生这种情况……目前至少有一些对这个问题无所适从的人,发现这篇文章可以同时做到这一点。

  1. Check if your ajax is executed 检查您的ajax是否已执行
  2. Check it's status. 检查它的状态。 If response code is != 200, than you should add error method also, for error handling. 如果响应代码为!= 200,则还应添加错误方法,以进行错误处理。

Try this: 尝试这个:

$.ajax({
   timeout: 20000,
   url: 'test.com',
   method: 'GET',
   crossDomain: true,
   dataType: 'jsonp',
   success: function (data) {
       console.log('callback success');
       this._cache = data;
       localStorage.token = data.access_token;
    }, 
    error: function(xhr, error){
       console.debug(xhr); console.debug(error);
    },
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM