jQuery Ajax Stop不被调用(无错误; 200 OK) [英] jQuery Ajax Stop is not invoked (No error; 200 OK)

查看:268
本文介绍了jQuery Ajax Stop不被调用(无错误; 200 OK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作的ASP.Net 2.0代码在我的开发服务器,使用jQuery Ajax。 ajax调用的结果用于加载下拉值。



但是当这个代码部署到新的DMZ服务器时,结果不会在下拉列表中填充 - 虽然我得到 200 OK 作为回应。一个明显的事情是 Type 在响应中是不同的。它预计为 application / json ,但以 text / plain 形式出现。



我有成功回调和错误回调代码。随着这个我有处理程序的ajax开始和停止。但这些事件都没有被解雇。什么原因错误 / 停止处理程序不会被解雇?



注意:IE和Chrome的行为是一样的。



strong>更新



也注意到控制台中记录了一个错误,如下所示。是与类型相关吗?我们如何解决这个问题?





注意:还要注意, Content-Length 在下面显示的响应标题中为0.



成功回拨





jQuery

  function loadASN()
{
var receiveScanParameter = getContainerParameters();

// console.log(receiveScanParameter);

$ .ajax({
type:POST,
url:rcvScanTXAdd.aspx / GetASNForPlant,
contentType:application / json; charset = utf-8,
dataType:json,
data:JSON.stringify({receiveScanParameter:receiveScanParameter}),
success:successPopulateASN,
error:errorFunction
});
}

全球jQuery代码
$ b

  //全局Ajax错误处理函数
function errorFunction(xhr,status,error)
{
if =='undefined'|| xhr == undefined)
{
alert('xhr undefined');
}

alert(status);
alert(error);
}


$(document).ready(function()
{
// Ajax开始
$ .ajaxStart(function()
{
//将光标改为等待
$(this).css({'cursor':'wait'})
});

// Ajax End
$('body')。ajaxStop(function(){

//重置光标
$(this)。 css({'cursor':'default'})

});



}

p>

解决方案

我想出来了。



步骤1 :发现浏览器控制台中记录错误(称结果为null)。 [此屏幕截图已在问题中更新]



步骤2 :观察到内容长度响应的c>为零。还会观察到响应中发生了 gzip 压缩(通过读取响应标头)。



步骤3 :分析服务器的web.config。它是一个C# httpModule 压缩。在httpModule中添加了 json 的旁路逻辑。 [我不想让json用这个自定义模块压缩。后来我将考虑添加压缩到JSON当我使用 IIS 压缩,而不是自定义模块]。以下是用于旁路JSON压缩的 C#代码段

 请求.ContentType.ToLower(CultureInfo.InvariantCulture).StartsWith(application / json)





I have a working ASP.Net 2.0 code in my development server that uses jQuery Ajax. The result of the ajax call is used to load dropdown values.

But when this code is deployed to a new DMZ server, the result is not getting populated in dropdown – though I am getting 200 OK as response. One obvious thing is that the Type is different in the response. It is expected as application/json but coming as text/plain.

I have success call back and error callback codes. Along with this I have handlers for ajax start and stop. But none of these events are getting fired. What is the reason error/stop handlers are not getting fired? How can we make it work?

Note: The behavior is same in both IE and Chrome.

Update

Also observed that there is an error logged in console, as shown below. Is it related to the "Type"? How can we address this?

Note: Also note that the Content-Length is 0 in the response headers shown below.

Success Callback

jQuery

      function loadASN()
      {
            var receiveScanParameter = getContainerParameters();

           // console.log(receiveScanParameter);

            $.ajax({
                    type: "POST",
                    url: "rcvScanTXAdd.aspx/GetASNForPlant",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ receiveScanParameter: receiveScanParameter }),
                    success: successPopulateASN,
                    error: errorFunction
                    });
      }

Global jQuery Code

    //Global Ajax Error handling Function
    function errorFunction(xhr, status, error) 
    {
              if(xhr == 'undefined' || xhr == undefined)
              {
                alert('xhr undefined');
              }

              alert(status);
              alert(error);
    }


 $(document).ready(function () 
 {
    //Ajax Start
    $('body').ajaxStart(function() 
    {
        //Change cursor to waiting 
        $(this).css({'cursor':'wait'})
    });

    //Ajax End
    $('body').ajaxStop(function() {

        //Reset the cursor
        $(this).css({'cursor':'default'})

    }); 



 });

Screenshots

解决方案

I figured it out.

Step 1: Observed that there is an error logged in browser’s console (saying result is null). [This screenshot is updated in the question]

Step 2: Observed that the content length of the response is zero. Also observed that there is a gzip compression happened on the response (by reading the response headers).

Step 3: Analyzed the server's web.config. It was uisng a C# httpModule for compression. In that httpModule added bypassing logic for json. [I don’t want json to be compressed with this custom module. Later I will consider adding compression to JSON when I use IIS for compression instead of custom module]. Following is the C# code segment for by-passing JSON compression

request.ContentType.ToLower(CultureInfo.InvariantCulture).StartsWith("application/json")


这篇关于jQuery Ajax Stop不被调用(无错误; 200 OK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆