简体   繁体   English

从此ajax调用获取http状态

[英]getting http status from this ajax call

I am really new to ajax and jquery, so please bear with me. 我对ajax和jquery真的很陌生,所以请多多包涵。 I am currently building a small application that will do the basic task of telling you a website's status code, and doing something based on the code that is returned. 我目前正在构建一个小型应用程序,该应用程序的基本任务是告诉您网站的状态代码,并根据返回的代码执行某些操作。 I found this code online, and it returns "Page not found" if the http status code is 404. 我在网上找到了此代码,如果http状态代码为404,它将返回“找不到页面”。

$(function() {
      var url = "http://www.google.com";
      $.ajax(url,
      {
         statusCode: {
         404: function() {
            alert('page not found');
         }
      }
   });   
});

But, instead of checking for a 404 status code, how can I save the raw status code value to a variable? 但是,如何不检查404状态代码,如何将原始状态代码值保存到变量?
Thanks! 谢谢!

Hope something like this might help u mate.. :) 希望这样可以帮助你交配.. :)

 error:function (xhr, ajaxOptions, thrownError){
          alert(xhr.status); 
           switch (xhr.status) {
              case 404:
                    // Desired Action.
              }
        } 

    complete: function(xhr, statusText){
           alert(xhr.status); 
    }

U may get quite lot of information in the below link mate 您可能会在以下链接伴侣中获得很多信息

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

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

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