简体   繁体   中英

Google Chrome Extension Http request

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM