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

jQuery.ajax fires success on 404 or any other errors

本文关键字:错误 成功 其他 任何 ajax jQuery      更新时间:2023-09-26

通过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);
    }
});

而CCD_ 1只包含:

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

令人惊讶地提醒success:404
(Mozilla/5.0(X11;Ubuntu;Linux i686;rv:28.0(Gecko/20100101 Firefox/28.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'); ?>