views:

135

answers:

1

Hello all,

I am trying to make an asynchronous request with POST method from a web worker used in my extension. The thing is that it does not work for me. On the server side I have PHP script listening for data in $_POST variable. Although I am able to establish connection to the server, and even pass some data in URL (GET), the $_POST is always empty.

Here is the latest code I'm using in the web worker:

var serviceUrl = "http://localhost/pfm/service/index.php";
var invocation = new XMLHttpRequest();
if(invocation){
    invocation.open('POST', serviceUrl, true);
    invocation.setRequestHeader('X-PINGOTHER', 'pingpong');
    invocation.setRequestHeader('Content-Type', 'text/plain');
    invocation.onreadystatechange = function(){processResponse(invocation);};
    invocation.send("action=init");
}

(borrowed from MDN web site when I got an idea that the issue was the same origin policy)

Prior to this I was using a rather obvious and ridiculously simple:

var serviceUrl = "http://localhost/pfm/service/index.php";
var xhr = new XMLHttpRequest();
xhr.open("POST", serviceUrl, true);
xhr.onreadystatechange = function(){processResponse(receiptStoreRequest);};
xhr.send("action=init");

In this case the request also passed, but the $_POST was still empty.

Is it possible that POST-requests are not allowed on web workers?

Right now everything is being tested on localhost.

A: 

Don't set the content type text/plain but

invocation.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

In case you're developing for Firefox 4+ you might also be interested in FormData objects

VolkerK
Thank you, it worked.
kooker