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