本文介绍了每个错误code显示响应codeS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让该做的是,以显示不同的错误信息,响应code 200 400 404 408 &放大器; 500 不过,我不认为我是这样做的权利。

我看着这一个:<一href="http://stackoverflow.com/questions/12734714/jquery-ajax-error-handling-http-status-$c$cs">jQuery还在迷茫.. AJAX错误处理(HTTP状态codeS)但

我下面可能是混乱的,但任何指导?

  VAR请求; //使Request对象的全局变量
            传播getAJAX(){
              要求=新XMLHtt prequest();
              request.open(GET,file.txt的);
              request.onreadystatechange = checkData;
              request.send(空);
            } //函数结束\

            传播getYo(){
              要求=新XMLHtt prequest();
              request.open(GET,FILE2.TXT);
              request.onreadystatechange = checkDataa;
              request.send(空);
            } //函数结束

            传播checkData(){
              如果(request.readyState == 4){
                //如果状态已完成
                如果(request.status == 200){
                  //如果尝试成功
                  警报(request.responseText);
                如果} //结束
              如果} //结束
            } //结束checkData

            功能checkDataa(){
              如果(request.readyState == 4){
                //如果状态已完成
                如果(request.status == 400){
                  //如果尝试成功
                  警报(request.responseText);
                如果} //结束
              如果} //结束
            } //结束checkData
 

解决方案

利用jQuery的 $。阿贾克斯() 状态code 设置对象,回调函数,尝试

编辑,更新

HTML,如,

 &LT;按钮ID =错误&GT;错误&LT; /按钮&GT;
&LT;按钮ID =成功&GT;成功&LT; /按钮&GT;
 

JS

  $(函数(){
    VAR的网址= [file.txt的,FILE1.TXT];
    VAR请求=功能(URL){
        返回$阿贾克斯({
        网址:网址,
        键入:GET,
        状态code:{
            200:功能(数据,textStatus,jqxhr){
                    警报(数据);
            },
            403:功能(jqxhr,textStatus,errorThrown){
                    警报(textStatus +\ N+ errorThrown);
            },
            404:功能(jqxhr,textStatus,errorThrown){
                    警报(textStatus +\ N+ errorThrown);
            }
        }
    });
    };

$(#键错误)。在(点击,功能(五){
    请求(网址[0]);
});
$(#按钮成功)。在(点击,功能(五){
    请求(网址[1]);
});

});
 

http://jsfiddle.net/guest271314/XSQ3w/

What i'm trying to get this to do is to display different error messages, response code 200, 400, 404, 408 & 500 however, i don't think i'm do it right.

i looked at this one: jQuery AJAX Error Handling (HTTP Status Codes) but was still confused..

What i have below is probably messy, but any guidance?

            var request;  //make request object a global variable
            function getAJAX(){
              request = new XMLHttpRequest();
              request.open("GET", "file.txt");
              request.onreadystatechange = checkData;
              request.send(null);
            } // end function\

            function getYo(){
              request = new XMLHttpRequest();
              request.open("GET", "file2.txt");
              request.onreadystatechange = checkDataa;
              request.send(null);
            } // end function

            function checkData(){
              if (request.readyState == 4) {
                // if state is finished
                if (request.status == 200) {
                  // and if attempt was successful
                  alert(request.responseText);
                } // end if
              } // end if
            } // end checkData

            function checkDataa(){
              if (request.readyState == 4) {
                // if state is finished
                if (request.status == 400) {
                  // and if attempt was successful
                  alert(request.responseText);
                } // end if
              } // end if
            } // end checkData
解决方案

Utilizing jquery $.ajax() statusCode settings object , callback function , try

Edit, updated

html , e.g.,

<button id="error">error</button>
<button id="success">success</button>

js

$(function() {
    var urls = ["file.txt", "file1.txt"];
    var request = function (url) {
        return $.ajax({
        url : url,
        type : "GET",
        statusCode : {
            200 : function (data, textStatus, jqxhr) {
                    alert(data);
            },
            403 : function (jqxhr, textStatus, errorThrown) {
                    alert(textStatus + "\n" + errorThrown);
            },
            404 : function (jqxhr, textStatus, errorThrown) {
                    alert(textStatus + "\n" + errorThrown);
            }
        }
    });
    };

$("button#error").on("click", function(e) {
    request(urls[0]);
});
$("button#success").on("click", function(e) {
    request(urls[1]);
});

});

jsfiddle http://jsfiddle.net/guest271314/XSQ3w/

这篇关于每个错误code显示响应codeS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 15:28