【问题标题】:jQuery.ajax fires success on 404 or any other errorsjQuery.ajax 在 404 或任何其他错误上触发成功
【发布时间】:2014-04-29 19:39:59
【问题描述】:

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

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

【问题讨论】:

  • 除了警报中的 Javascript 错误(您应该将“=”更改为“+”),我没有发现任何问题。该代码实际上为我提醒“错误:404”。 Windows 7、Chrome 和 Firefox(最新版本)。你有没有测试过这个php函数来改变响应码:php.net/manual/en/function.http-response-code.php
  • @yvanavermaet = 只是一个错误并已编辑。我测试了http_response_code(404);,但仍然存在同样的问题。

标签: javascript php jquery ajax


【解决方案1】:

我复制粘贴了您的代码,它运行良好。输出“错误: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'); ?>

【讨论】:

  • 该代码以前可以运行,恐怕这是我的 Ubuntu 操作系统上的未知问题。由于突然拔掉电源线而导致硬件重置后才发生这种情况,但我并不认为这是合乎逻辑的原因。但是,我的操作系统和 PHP 似乎损坏了!
  • @Omid 我不知道...我正在使用 Mint 15 和 Firefox 26。不知道为什么它会与您的操作系统相关。
猜你喜欢
  • 1970-01-01
  • 2020-09-21
  • 1970-01-01
  • 2018-08-05
  • 2020-07-15
  • 2020-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多