简体   繁体   中英

jQuery.ajax fires success on 404 or any other errors

A simple Ajax request via jQuery:

$.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);
    }
});

while ajax.php just contains:

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

surprisingly alerts success:404 !
(Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0)

Has a bug been reported for this issue before, or I'm doing something wrong?

I copy-pasted your code and it works just fine. Outputs "error: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'); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM