简体   繁体   中英

Error when sending XHR from Chrome Extension to a PHP page: Cross origin requests are only supported for HTTP

I want to send an XHR (using GET method) from a Chrome Extension to a PHP page.But when I try to run the code, I get an error:

    "Cross origin requests are only supported for HTTP"
    "XMLHttpRequest Exception 101"

The PHP page loads fine when I call it directly in the browser, so being unable to locate the file would not cause the error. Due to these errors I am unable to send data from the extension to the PHP page to store in the database.I have included https://*/ in the manifest.json (under permissions ) of the extension, but it still does not work.

Please let me know how to overcome this error and send the request successfully.

PS: The page on which the extension is running and sending the XHR is a HTTPS page, while the PHP is a HTTP page.Could this be the cause of problem.If yes how to overcome this.

I assume your doing this in a Content-Script, if so, then you cannot. It will be treated to the same origin policy. If you want to use XHR requests in a Chrome Extension, place it in your extension pages (Background page).

And when you do, make sure you add the the proper match patterns under permission:

http://code.google.com/chrome/extensions/match_patterns.html

For example, https://*/ should be https://*/* and your XHR should be in a background page where your content script communicates with it through messaging .

I know its a bit old but just in case someone comes across this page. This is a replica of this question .

You can make cross-origin-xhr from your content scripts if you have proper permissions declared.

You cannot. XHR is subject to the same origin policy .

You could use JSONP instead though.

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