weixin_33699914 2012-10-06 12:05 采纳率: 0%
浏览 376

jQuery ajax http状态码

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条回答

  • weixin_33735077 2012-10-06 12:07
    关注

    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{
    
           }
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示