问题描述
我想知道Google Chrome扩展程序是否可以发出HTTP请求并解析结果的正文(如Curl)。例如,有一个服务器1.2.3.4通过总结URL参数来回答问题?a = 1& b = 2
。查询http://1.2.3.4?a=1&b=2
会返回包含 3 ,我的扩展想要提交这样的查询和解析结果。
任何帮助将不胜感激。
是的,使用。在清单中设置权限并像使用它一样
var xhr = new XMLHttpRequest();
xhr.open(GET,http://api.example.com/data.json,true);
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
//警告!可能会注入恶意脚本!
document.getElementById(resp)。innerHTML = xhr.responseText;
...
}
}
xhr.send();
I'd like to know if Google Chrome Extension can make a HTTP request and parse the body of the result (like Curl). For example, there is a server 1.2.3.4 that answers the question ?a=1&b=2
by summarizing the URL parameters. The query "http://1.2.3.4?a=1&b=2"
would return a body containing 3
, and my extension wants to submit such a query and parse the result.
Any help would be appreciated.
解决方案 Yes, using Cross-Origin XMLHttpRequest. Set the permissions in the manifest and use it like
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://api.example.com/data.json", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// WARNING! Might be injecting a malicious script!
document.getElementById("resp").innerHTML = xhr.responseText;
...
}
}
xhr.send();
这篇关于Google Chrome扩展程序Http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!