Google Chrome Extension Http request

孤者浪人 提交于 2019-12-30 11:10:54

问题


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.


回答1:


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();


来源:https://stackoverflow.com/questions/11506687/google-chrome-extension-http-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!