开发者

How to create a JSON post request in firefox extension?

I am trying to call the Google API, a JSON post request from a Firefox extension, e.g.

POST https://www.googleapis.com/urlshortener/v1/url
Content-Type: application/开发者_运维技巧json

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

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


Simplest way is to use XMLHttpRequest, exactly as you would do from a web page (only that a web page is limited by the same-origin policy).

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/"}');

To serialize and parse JSON see https://developer.mozilla.org/En/Using_native_JSON.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜