如何测试全局AJAX错误处理

时间:2015-10-30 18:30:48

标签: jquery ajax

我通过这种方式设置了全局AJAX错误处理功能,但我不知道如何测试它?无论如何在jsFiddle

  $(function () {
      var statusErrorMap = {
        '400' : "Server understood the request, but request content was invalid.",
        '401' : "Unauthorized access.",
        '403' : "Forbidden resource can't be accessed.",
        '500' : "Internal server error.",
        '503' : "Service unavailable."
      };
      //setup ajax error handling
      $.ajaxSetup({
          error: function (x, status, error) {
              if  (x.status in statusErrorMap) {
                if (x.status == "503") {
                    alert("Sorry, your session has expired. Please login again to continue");
                    window.location.href ="/users/sign_in";
                } else {
                  alert("("+ x.status +") error occurred: " + statusErrorMap[x.status.toString()]);
                }
              }
              else {
                alert("An error occurred: " + status + "nError: " + error);
              }              
          }
      });
  });

1 个答案:

答案 0 :(得分:0)

您必须使用fiddler(代理工具)拦截服务器请求并测试您的错误处理代码。 为此,您必须在fiddler中为服务调用设置断点,并按照以下过程进行操作。

  1. 规则>自动断点>回应后
  2. 在quickexec框(底部的黑框)中输入“bpafter yourpage.svc”。现在,Fiddler将在对包含“yourpage.svc”的任何URL的所有请求之前停在断点处。键入“bpafter”,不带参数以清除断点。
  3. 使用FiddlerScript以编程方式篡改响应。 FiddlerScript的最佳文档位于官方网站:http://www.fiddler2.com/Fiddler/dev/
  4. 您可以在下面的stackoverflow中获取additioanl详细信息。

    How do I use Fiddler to modify the status code in an HTTP response?