开发者

How to create a JSON post request in firefox extension?

开发者 https://www.devze.com 2023-03-10 02:09 出处:网络
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

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

精彩评论

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

关注公众号