How to create a JSON mail request in a Firefox extension?

I am trying to call the Google API, JSON mail request from a Firefox extension, for example.

POST https://www.googleapis.com/urlshortener/v1/url
Content-Type: application/json

{"longUrl": "http://www.google.com/"}

How can I name this API and handle the response in a Firefox extension?

+3
source share
1 answer

The easiest way is to use XMLHttpRequest, just like you would from a web page (only so that the web page is limited to policies of the same origin).

var request = new XMLHttpRequest();
request.open("POST", "https://www.googleapis.com/urlshortener/v1/url");
request.setRequestHeader("Content-Type", "application/json");
request.overrideMimeType("text/plain");
request.onload = function()
{
    alert("Response received: " + request.responseText);
};
request.send('{"longUrl": "http://www.google.com/"}');

For serialization and analysis of JSON, see https://developer.mozilla.org/En/Using_native_JSON .

+2
source

All Articles