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

标签 ajax json firefox firefox-addon google-api

我正在尝试调用 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 .

关于ajax - 如何在 firefox 扩展中创建 JSON post 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6231346/

相关文章:

javascript - 使用ajax提交表单

c# - Ajax HtmL 编辑器图片上传 JSON 错误

Firefox 和 IE 中的 jQuery UI 可调整大小错误(?)

javascript - Firefox 中未对齐的 Bootstrap Carousel 图像

javascript - 可能有一个独立的 JS/HTML (jQuery) 文件,在本地模拟 AJAX 调用?

javascript - 表单数据未发送到服务器

python - TypeError : jsonify() argument after ** must be a mapping, 未列出使用 Flask 返回的 JSON

json - JSON 的 XPath 等效项

javascript - Firefox 中的多个 CSS 伪选择器不起作用

javascript - 更改 ajax 以请求用要嵌入到 html 页面本身的 html 内容填充 div