PHP Jquery Ajax 调用抛出 net::ERR_EMPTY_RESPONSE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22284111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Jquery Ajax call throws net::ERR_EMPTY_RESPONSE
提问by webblover
I have the following JavaScript code in the page. When the ajax call is made, I could see that the browser inspect/debugger section throws net::ERR_EMPTY_RESPONSEerror. It works fine in localhost environment but throws above error in production.
我在页面中有以下 JavaScript 代码。进行 ajax 调用时,我可以看到浏览器检查/调试器部分抛出net::ERR_EMPTY_RESPONSE错误。它在 localhost 环境中工作正常,但在生产中引发上述错误。
In client side code,
在客户端代码中,
<script>
$(document).ready(function(){
$("#identityLinks a").on("click", function(){
value = $(this).attr("id");
if(value != "")
{
$.ajax({
url: "publish/updateUsersData.php",
type: "POST",
data: {receiverId: value},
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType: "json",
success: function(data) {
//alert(data["result"]);
console.log(data["result"]);
},
error: function(xhr, textStatus, errorThrown) {
//alert(xhr +" "+ textStatus +" "+errorThrown);
console.log(xhr +" "+ textStatus);
}
});
}
});
</script>
In Server-side code (PHP), I have the following code in updateUsersData.php:
在服务器端代码(PHP)中,我在updateUsersData.php 中有以下代码:
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 1996 00:00:00 GMT');
header('Content-type: application/json; charset=UTF-8');
if(isset($_POST["receiverId"]))
{
$receiver_id = trim($_POST["receiverId"]);
$arr = array("result"=>$receiver_id);
echo json_encode($arr); die();
break;
}
else
{
$arr = array("result"=>"No user data received. Error!");
echo json_encode($arr); die();
break;
}
?>
Do you think it's due to header with Expire calls or a bug in Jquery 1.9.1 version? I didn't find such errors when we were previous versions. Also, this code has not been updated for 5 months and browser errors creep just some time ago. Thanks for all your help and support.
您认为这是由于带有 Expire 调用的标头还是 Jquery 1.9.1 版本中的错误?我们以前的版本的时候没有发现这样的错误。此外,这段代码已经有 5 个月没有更新了,而且浏览器错误就在前一段时间蔓延。感谢大家的帮助和支持。
Edit:
编辑:
Status: This ISSUEis not resolved so far. Jquery AJAXexperts' help needed. Anyone, please promote this question. Also, CORSis not responsible for this issue.
状态:此问题目前尚未解决。需要 Jquery AJAX专家的帮助。任何人,请宣传这个问题。此外,CORS不对这个问题负责。
When clicking on the console in which the above error occurred, I was directly taken to this line in
当点击出现上述错误的控制台时,我直接被带到了这一行
JQuery 1.9.1 where console error lines point to:
=> xhr.send( ( s.hasContent && s.data ) || null );
Also these ones are shown in console error mark:
=> transport.send( requestHeaders, done );
=> ret = ( (jQuery.event.special[ handleObj.origType ] || {}).handle
|| handleObj.handler )
.apply( matched.elem, args );
=> return typeof jQuery !== core_strundefined &&
(!e || jQuery.event.triggered !== e.type) ?
jQuery.event.dispatch.apply( eventHandle.elem, arguments ) : undefined;
Is jquery latest version responsible for this PROVISIONAL HEADERS ARE SHOWN error.
是 jquery 最新版本负责此 PROVISIONAL HEADERS ARE SHOWN 错误。
回答by Mister P
I believe your request is not classed as a "simple request" under the CORS specification:
我相信您的请求在 CORS 规范下不被归类为“简单请求”:
http://www.w3.org/TR/cors/#simple-cross-origin-request-0
http://www.w3.org/TR/cors/#simple-cross-origin-request-0
since you are setting a response header for Content-Type: application/json.
因为您正在为 Content-Type: application/json 设置响应标头。
So your server will need to handle the preflight request, which involves setting some additional headers on both the request and response for the CORS request to be successful
因此,您的服务器将需要处理预检请求,这涉及在请求和响应上设置一些额外的标头以使 CORS 请求成功
Here is a good article on what you need to do - check out the bit under "not so simple":
这是一篇关于您需要做什么的好文章 - 查看“不那么简单”下的部分:
回答by Amir Hossein Jamsidi
I have same problem on my page sometimes. I think this happens because of number of variables or how big they are. I have a page that sends a json of about 250 variables without any problem, but this error occurs in this page while about 1500 variables are to be sent. This problem is new. Every thing even with 3000 variables were without problem, before. This problem occurs for me both in chrome and firefox in recent versions. It's not server side error because I have configured apache to revive 1 million variables of 30 Mb data.
我的页面有时也有同样的问题。我认为这是因为变量的数量或它们有多大。我有一个页面可以发送大约 250 个变量的 json 没有任何问题,但是这个错误发生在这个页面上,同时要发送大约 1500 个变量。这个问题是新的。以前,即使有 3000 个变量,每件事也没有问题。在最近版本的 chrome 和 firefox 中,我都会出现这个问题。这不是服务器端错误,因为我已将 apache 配置为恢复 30 Mb 数据的 100 万个变量。
回答by amorimluc
I was having this same error and was able to fix it by sending all the data I need to process in an array to the server, and then having it spit the updated array back to the client, so that only one AJAX call is made.
我遇到了同样的错误,并且能够通过将我需要在数组中处理的所有数据发送到服务器来修复它,然后让它将更新的数组吐回客户端,以便只进行一次 AJAX 调用。
I wasn't able to determine the exact cause of this issue, but I'm pretty sure what's going on is that some sort of buffer is getting filled to capacity with all those AJAX calls, causing the server to shut down some of those requests.
我无法确定此问题的确切原因,但我很确定正在发生的事情是所有这些 AJAX 调用都填满了某种缓冲区,导致服务器关闭了其中一些请求.
Hope this helps.
希望这可以帮助。
回答by James
So this can happen for a number of reasons regardless of language or technology being used. I have encountered this with extremely small payloads of 15-20 data points being sent back and forth using angular resource. PHP, Jquery, Angular all handle the error differently but the results down to one of three things happening.
因此,无论使用何种语言或技术,都可能出于多种原因而发生这种情况。我遇到过这种情况,使用 angular 资源来回发送 15-20 个数据点的极小负载。PHP、Jquery、Angular 都以不同的方式处理错误,但结果归结为发生的三件事之一。
Latency or Network issues, I have had this happen on extremely slow connections, again this could be anything to a bad router or improper firewall configuration to just on a crappy network, to programs like avast that put a proxy in place to protect you from viruses.
High server load, I have forced a server to be at 100% cpu from stress testing and this will happening on random calls because the server is not handing the requests fast enough. This is hard to fix and really is a scaling issue for the system.
Huge Data Sets, large items being sent that the response fails, usually this can be fixed by adjusting your timeout settings on whatever web server you are using.
延迟或网络问题,我在极慢的连接上发生过这种情况,同样,这可能是由于路由器故障或防火墙配置不当,仅在糟糕的网络上,也可能是像 avast 这样放置代理以保护您免受病毒侵害的程序.
高服务器负载,我已经通过压力测试迫使服务器处于 100% cpu,这将发生在随机调用中,因为服务器处理请求的速度不够快。这很难解决,而且确实是系统的扩展问题。
巨大的数据集,发送响应失败的大项目,通常可以通过在您使用的任何 Web 服务器上调整超时设置来解决此问题。
回答by Kevin
I have been able to resolve this in most cases with a simple function that I wrote to dump/reset all garbage memory and flush/reset the output memory after any/all loops.
在大多数情况下,我已经能够通过一个简单的函数来解决这个问题,我编写了该函数来转储/重置所有垃圾内存并在任何/所有循环后刷新/重置输出内存。
function memes(){
ob_flush();
ob_end_flush();
gc_collect_cycles();
ob_start();
gc_enable();
}
回答by Erhan Rden
I had same problem. i tried many solution advice but nothing worked. Finally i was able to fix this issue by using Ajax Get method instead post. But prefer this if you dont send sensitive data.
我有同样的问题。我尝试了很多解决方案的建议,但没有任何效果。最后我能够通过使用 Ajax Get 方法而不是 post 来解决这个问题。但如果您不发送敏感数据,则更喜欢这个。
回答by Hasan Wajahat
I was also getting the same error while using XMLHttpRequest().
我在使用 XMLHttpRequest() 时也遇到了同样的错误。
However I was able to resolve the error by disabling memory limit for the script on the server side.
但是,我能够通过在服务器端禁用脚本的内存限制来解决该错误。
This is done by setting memory_limit to -1 in the php.ini file. By default it is set to 128M, so you may also try increasing the memory size rather than completely disabling it as it may take a lot of server's resources.
这是通过在 php.ini 文件中将 memory_limit 设置为 -1 来完成的。默认情况下它设置为 128M,因此您也可以尝试增加内存大小而不是完全禁用它,因为它可能会占用大量服务器资源。
回答by user1752752
I found same issue due to my network problem, may be your network also causing this.
由于我的网络问题,我发现了同样的问题,可能是您的网络也导致了这个问题。

