如何创建Firefox扩展的JSON POST请求? [英] How to create a JSON post request in firefox extension?

查看:117
本文介绍了如何创建Firefox扩展的JSON POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打电话给谷歌API,从一个Firefox扩展的JSON职位要求,如:

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/"}

我怎么能调用此API和处理的Firefox扩展的反应?

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

推荐答案

最简单的方法是使用XMLHtt prequest,正是因为你会做从网页(只有一个网页是由相同来源的限制政策)。

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

要序列化和解析JSON看到 https://developer.mozilla.org/En/Using_native_JSON

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

这篇关于如何创建Firefox扩展的JSON POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆