jQuery ajax http状态码


                    

目前,我正在将脚本更改为jquery。 这是我的旧javascript:</ p>

  var request =(window.XMLHttpRequest)吗?  new XMLHttpRequest():(window.ActiveXObject?new window.ActiveXObject(“ Microsoft.XMLHTTP”):false);
request.open(“ GET”,url,true);
request.onreadystatechange = updatePage;
request.send(null);

函数updatePage(){
         如果(request.readyState == 4){
                 如果(request.status == 200){
                     var response = request.responseText;
                     做一点事();
                 } else if(request.status == 304){
                     doSomethingElse();
                 }其他{
                 }
         }
}
</ code> </ pre>

我现在想将其更改为jquery-ajax:</ p>

  request = $ .ajax({
     类型:“ GET”,
     网址:url,
     数据:数据,
     成功:updatePage
});
</ code> </ pre>

如何像在旧脚本中一样测试请求返回的状态代码(和responseText)?</ p>
     </ div>

展开原文

原文

I am changing my scripts to jquery at the moment. This is my old javascript:

var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : (window.ActiveXObject ? new window.ActiveXObject("Microsoft.XMLHTTP") : false);
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);

function updatePage() {
        if (request.readyState == 4) {
                if (request.status == 200) {
                    var response = request.responseText;
                    doSomething();
                } else if (request.status == 304) {
                    doSomethingElse();
                } else {
                }
        }
}

I now want to change this to jquery-ajax:

request = $.ajax({
    type: "GET",
    url: url,
    data: data,
    success: updatePage
});

How can I test for the status code (and the responseText) returned by the request, like I did in my old script?

2个回答


尝试:</ p>

  request = $ .ajax({
     类型:“ GET”,
     网址:url,
     数据:数据,
     statusCode:{
       200:function(){
         做一点事();
        },
       304:function(){
         doSomethingElse();
        }
      }
});
</ code> </ pre>

或:</ p>

  request = $ .ajax({
     类型:“ GET”,
     网址:url,
     数据:数据,
     完整:功能(e,xhr,设置){
        if(e.status === 200){

        }否则if(e.status === 304){

        }其他{

        }
     }
});
</ code> </ pre>
     </ div>

展开原文

原文

try this:

request = $.ajax({
    type: "GET",
    url: url,
    data: data,
    statusCode: {
      200: function() {
        doSomething();
       },
      304:function(){
        doSomethingElse();
       }
     }
});

or:

request = $.ajax({
    type: "GET",
    url: url,
    data: data,
    complete: function(e, xhr, settings){
       if(e.status === 200){

       }else if(e.status === 304){

       }else{

       }
    }
});


您只需要将此请求绑定到按钮单击之类的事件即可
喜欢</ p>

  $('#btn')。click({function(){
$ .ajax({
     类型:“ GET”,
     网址:url,
     数据:数据,
     成功:updatePage
});
});
</ code> </ pre>

您不需要真正针对某个变量保留此请求,因为 jQuery ajax </ strong>具有成功回调</ strong>,它将帮助您执行发布成功代码</ p>
     </ div>

展开原文

原文

You need to just bind this request to an event like button click or something Like

$('#btn').click({function(){
$.ajax({
    type: "GET",
    url: url,
    data: data,
    success: updatePage
});
});

You dont need to really keep this request against a variable as jQuery ajax has a success callback which will help you to execute post success code

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问