如何在 firefox 扩展中创建 JSON post 请求?

2024-02-10

我正在尝试调用 Google API,这是来自 Firefox 扩展的 JSON post 请求,例如

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

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

如何在 Firefox 扩展中调用此 API 并处理响应?


最简单的方法是使用 XMLHttpRequest,就像在网页中所做的那样(只是网页受到同源策略的限制)。

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 https://developer.mozilla.org/En/Using_native_JSON.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 firefox 扩展中创建 JSON post 请求? 的相关文章

随机推荐