HTTP POST in javascript in Firefox Extension

2020-02-15 07:35发布

问题:

I'm a newbie trying to do a simple HTTP post in JS within a Firefox extension..

This isn't passing through the parameters:

var params = "a=1&b=2&c=3"
req.open('POST', 'http://www.mywebsite.com/');
req.send(params);

Any ideas on what I'm doing wrong? Thanks.

回答1:

Make sure you've included the header to tell the server what type of request body you're sending it:

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

(Assuming req is an XMLHttpRequest created earlier in the code.)



回答2:

Make sure you add

var req = new XMLHttpRequest(); 


回答3:

You don't need an extension, plain JavaScript can do this:

Asynchronous cross-domain POST request via JavaScript?