zoukankan      html  css  js  c++  java
  • How to handle error In $.get()

    Using $.ajaxSetup is global for all ajax calls. Because the $.get function doesn't have any error callbacks, defining an error handler in $.ajaxSetup is the only way to handle errors. If you use $.ajax, you can define the error handler in the $.ajax call like this $.ajax({ url: "HTMLPage.htm", success: function(data) { alert(data); $('#mydiv').html(data); }, error: function(XMLHttpRequest, textStatus, errorThrown) { if (XMLHttpRequest.status == 0) { alert(' Check Your Network.'); } else if (XMLHttpRequest.status == 404) { alert('Requested URL not found.'); } else if (XMLHttpRequest.status == 500) { alert('Internel Server Error.'); } else { alert('Unknow Error.\n' + XMLHttpRequest.responseText); } } }); This is specific to only this ajax call, that way you can have more specific error messages. But using the global error handler works just as well. You could define your functions outside of the $(document).ready() like this $(document).ready(function() { $.ajaxSetup({ error: AjaxError }); $.get("HTMLPage.htm", GetSuccess); }); function AjaxError(x, e) { if (x.status == 0) { alert(' Check Your Network.'); } else if (x.status == 404) { alert('Requested URL not found.'); } else if (x.status == 500) { alert('Internel Server Error.'); } else { alert('Unknow Error.\n' + x.responseText); } } function GetSuccess(data) { alert(data); $('#mydiv').html(data); }
  • 相关阅读:
    服务器图片等资源在8080端口保存
    thinkphp 3.2.1 URL 大小写问题 下面有具体说明
    linux samba smb 在客户端无法连接使用
    php连接redis服务
    服务器死机 导致 mongo 挂掉
    同一个页面引用不同版本jquery库
    CSS3阴影 box-shadow的使用和技巧总结
    php 中使用正则
    Hbase-1.1.1-java API
    hive1.2.1问题集锦
  • 原文地址:https://www.cnblogs.com/easyteck/p/2553789.html
Copyright © 2011-2022 走看看