Richard Bernstein
Richard Bernstein

Reputation: 371

getting a server 500 error during a CORs response

I am trying to send a JSON buffer from a Chrome extension but am getting an internal 500 error. Here is the code that sends the buffer.

var xhr = new XMLHttpRequest();
xhr.onerror = function(req,textStatus,errorThrown) {
    console.log(textStatus);
    alert('on-error')};

    xhr.open('POST', url, true);
    xhr.setRequestHeader("Content-type", 'application/json');
    xhr.setRequestHeader("X-Requested-With",'XMLHttpRequest');
    xhr.setRequestHeader("Access-Control-Allow-Origin", '*');
	
    xhr.onload = function () {   //response will go here
        if(xhr.status == 500) {
            alert("server error 500");
        }

On the Apache Linux server side, I send back a response with

enter code here
       $this->output
            ->set_content_type('application/json')
            ->set_header("Access-Control-Allow-Origin", "*")
            ->set_output(json_encode($table));

I have been stuck on this problem for over a month and really need some ideas on what I am doing wrong. I have debuggers on both machines. I see that the buffer is going out correctly and I can see it is being received correctly. I think I am setting the response header correctly but I always get a 500 error. BTW, it works fine with a WAMP running on my localhost.

This is from the apache error_log: [Fri Oct 26 18:22:07.162178 2018] [proxy_fcgi:error] [pid 2001:tid 139930806171392] [client 69.124.178.164:57141] malformed header from script 'index.php': Bad header: {"0":["{choose}","IOS","Mac"," [Fri Oct 26 18:22:07.163669 2018] [proxy_fcgi:error] [pid 2001:tid 139930806171392] [client 69.124.178.164:57141] AH01070: Error parsing script headers [Fri Oct 26 18:22:07.163679 2018] [proxy_fcgi:error] [pid 2001:tid 139930806171392] (22)Invalid argument: [client 69.124.178.164:57141] AH01075: Error dispatching request to : [Fri Oct 26 18:22:33.796554 2018] [proxy_fcgi:error] [pid 2001:tid 139930797778688] [client 69.124.178.164:57179] malformed header from script 'index.php': Bad header: {"0":["{choose}","IOS","Mac"," [Fri Oct 26 18:22:33.796627 2018] [proxy_fcgi:error] [pid 2001:tid 139930797778688] [client 69.124.178.164:57179] AH01070: Error parsing script headers [Fri Oct 26 18:22:33.796633 2018] [proxy_fcgi:error] [pid 2001:tid 139930797778688] (22)Invalid argument: [client 69.124.178.164:57179] AH01075: Error dispatching request to :

The line that says malformed header is actually my payload from the server to the Extension. Is is not a header, it is the payload.

Upvotes: 3

Views: 5167

Answers (1)

Richard Bernstein
Richard Bernstein

Reputation: 371

Well after over a month of thinking it was my server or my code, it turned out to to be a unconfirmed bug in codeigniter. I replaced

$this->output
          ->set_content_type('application/json')
          ->set_header("Access-Control-Allow-Origin", "*")
          ->set_output(json_encode($table));

with direct php calls:

header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
echo ($json);

Now it works perfectly!

Upvotes: 4

Related Questions