0

使用jQueryand时Ajax,我将整个 URL 传递给 ajaxurl:以防万一URL不存在(不知何故),我怎么page not found能从内部重定向到页面ajax?通过url.htaccess归档。

$(document).ready(function() {
    $('.link').click(function () {
    var parentTag = 'check=checked';
    if(parentTag) {
        // ajax call
        $.ajax({
            type:"GET",
            url:"/mylink/buyers/1/0/0", //If this failed, how do I redirect to something else from here?
            data:parentTag,
        dataType: 'json',
            beforeSend:function(callBack){
                console.log('checked');
            },
            success: function(callBack){
                console.log('Success');
            },
            error: function(callBack){
                console.log('Failed');
            },
        });
    }
    return false;
    });
});    
4

2 回答 2

2

只需为 404 或您想要的任何其他HTTP 状态添加statusCode处理程序!

您也可以添加一个通用的错误回调,但使用状态码方法的优势在于区分服务器的实际 404(未找到页面错误)和其他类型的连接问题。

$.ajax({
  /* ... */
 statusCode: {
    404: function() {
         // server says: page not found; let's redirect to 404 page
         window.location.href = "http://example.com/my/404/page";
    }
  },
  error: function() {
      // something else went wrong
      alert('An unknown error occurred!');
  }
});
于 2013-09-22T13:41:35.180 回答
2

只需放入window.location错误回调:

$(document).ready(function() {
    $('.link').click(function () {
    var parentTag = 'check=checked';
    if(parentTag) {
        // ajax call
        $.ajax({
            type:"GET",
            url:"/mylink/buyers/1/0/0", //If this failed, how do I redirect to something else from here?
            data:parentTag,
        dataType: 'json',
            beforeSend:function(callBack){
                console.log('checked');
            },
            success: function(callBack){
                console.log('Success');
            },
            error: function(callBack){
                console.log('Failed');
                window.location = "http://www.example.com";
            },
        });
    }
    return false;
    });
});
于 2013-09-22T13:42:13.090 回答