jQuery.ajax在404或任何其他错误上触发成功

时间:2014-04-29 19:39:59

标签: javascript php jquery ajax

通过jQuery的简单Ajax请求:

$.ajax({
    url: 'ajax.php', data: {}, type: 'post', async: false,
    success: function(result, status, XHR){ 
        alert('success:'+XHR.status);
    }, error: function(XHR, status, error){ 
        alert('error:'+XHR.status);
    }
});

ajax.php只包含:

<?php header('HTTP/1.1 404'); ?>

令人惊讶地提醒success:404
(Mozilla / 5.0(X11; Ubuntu; Linux i686; rv:28.0)Gecko / 20100101 Firefox / 28.0)

之前是否有针对此问题的报告错误,或者我做错了什么?

1 个答案:

答案 0 :(得分:0)

我复制粘贴你的代码,它运行得很好。输出“错误:404”

test.php的:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="//code.jquery.com/jquery-latest.min.js"></script>
<script>
$.ajax({
    url: 'ajax.php', data: {}, type: 'post', async: false,
    success: function(result, status, XHR){ 
        alert('success:'+XHR.status);
    }, error: function(XHR, status, error){ 
        alert('error:'+XHR.status);
    }
});
</script>
<title>Test</title>
</head>
<body>

<p>Hello</p>

</body>
</html>

ajax.php

<?php header('HTTP/1.1 404'); ?>