29

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_RESPONSE error. It works fine in localhost environment but throws above error in production.

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
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.

Edit:

Status : This ISSUE is not resolved so far. Jquery AJAX experts' help needed. Anyone, please promote this question. Also, CORS is not responsible for this issue.

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.

webblover
  • 1,196
  • 2
  • 12
  • 30
  • Has anybody got any clue to solve this issue or need more information? – webblover Mar 09 '14 at 15:41
  • I'm answering a comment you left **[here](http://stackoverflow.com/questions/21177387/caution-provisional-headers-are-shown-in-chrome-debugger/21179105#21179105)**. I don't think your problem is the **"Provisional Headers Are Shown"** **message**. As far as I can tell, that message does not represents an error, just shows that Chrome hasn't received real headers for that request. – Willington Vega Mar 24 '14 at 21:25
  • The **net::ERR_EMPTY_RESPONSE** and **Provisional Headers Sent - No actual request sent** errors you have mentioned should be more related to your problem. Is there a possibility we can see your system running? It seems the problem is not in the code, but in the client-server connection. – Willington Vega Mar 24 '14 at 21:33
  • I'm out of ideas here, so I'm just gonna ask simple questions hoping to hit something :). You are using a relative URL as the parameter to your Ajax call. That URL could be interpreted differently in production and development, depending on your configuration. Are you sure the request is being made to the right location? – Willington Vega Mar 24 '14 at 21:38
  • thank you @wvega for taking time to look into this issue. Yes, as you said, there doesn't seem to be problems in the code. It actually works well in localhost but throws above **error in production**. I also cross-checked the URL, it is pointing to correct location. I am still confused why this happen. **Upvote this question** (it becomes more visible) because this is a nuisance for many devs, I have seen many threads posting similar questions. – webblover Mar 25 '14 at 11:22
  • I've got the same problem, but it's a bit more complicated. On my local version everything is fine. On my online version, it's working too. But now the problem, my business partner wants to review the online version and he gets this issue. So I can review online without any problems, the API logs my requests, but on his computer (using Chrome, Chome Canary and Safari) the net::ERR_EMPTY_RESPONSE error is thrown and the API doesn't even log getting a request... – gidomanders Oct 23 '14 at 18:59
  • My business partner logged in as guest on his own computer, tried again and had no problems. Apparently there's something (like an app) on his computer that blocked certain requests. It's not clear what's causing this problem, but maybe this info helps you with your problem ;) – gidomanders Oct 23 '14 at 20:27
  • Happens to me too but the other way around. On production it's fine, on localhost it doesn't work most of the time. I don't have a clue, tried going step by step and sometimes it starts working without changing a thing. Weird. – trainoasis Apr 03 '18 at 13:56
  • Have you found a solution to this issue? I'm currently dealing with the same issue but with jQuery AJAX making a POST call to a RestAPI application. – John Odom Dec 09 '20 at 18:31

9 Answers9

4

I believe your request is not classed as a "simple request" under the CORS specification:

http://www.w3.org/TR/cors/#simple-cross-origin-request-0

since you are setting a response header for 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

Here is a good article on what you need to do - check out the bit under "not so simple":

http://www.html5rocks.com/en/tutorials/cors/

Mister P
  • 1,253
  • 1
  • 11
  • 9
  • 3
    thank you so much for pointing out Cross-Origin Resource Sharing (CORS). I will check out the options to see if the problem resolves. One other thing to note is we **DO NOT HAVE ANY ISSUE IN localhost environment**. Everything works fine. This problem occurs only in production. – webblover Mar 16 '14 at 17:03
  • We have also declared json data type in both client and server side. Don't really know where problem lies. This code complies with Jquery doc yet it throws Provisional Headers Sent - No actual request sent error. If someone could catch the bug in the code/somewhere else? – webblover Mar 16 '14 at 17:16
4

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.

Amir Hossein Jamsidi
  • 1,980
  • 1
  • 18
  • 10
4

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.

  1. 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.

  2. 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.

  3. 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.

James
  • 1,514
  • 12
  • 30
2

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.

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.

Hope this helps.

amorimluc
  • 1,661
  • 5
  • 22
  • 32
1

I was also facing the same error recently using AJAX XMLHttpRequest() as a function

function sysAJAX(inUrl,inData){
    return  $.ajax({type:"POST", url: inUrl, data: inData, cache:false,timeout:0, success:function(feed){},error:function(XMLHttpRequest,textStatus,errorThrown){}});
}

In my case it was a server side issue with PHP. I figured out that the data transported by json_encode() was too large and exceeded the PHP memory limit. As suggested by @Hasan Wajahat, adjusting the memory limit to me was not a good idea so I reviewed the entire data that was going to be sent by json_encode and this fixed the issue.

Big Pee
  • 41
  • 7
0

I found same issue due to my network problem, may be your network also causing this.

0

I was also getting the same error while using 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.

Hasan Wajahat
  • 1,717
  • 2
  • 16
  • 16
0

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();
}
Kevin
  • 331
  • 2
  • 5
0

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.

Erhan Rden
  • 231
  • 1
  • 3
  • 7