简体   繁体   中英

Chrome Extension - Not allowed by Access-Control-Allow-Origin

I am trying to create a Google Chrome extension, but when I submit my form, I get the following error:

XMLHttpRequest cannot load http://www.domain.com/?i=ajax No "Access-Control-Allow-Origin".

I have checked in the extension list under "Permissions", and I see my website listed there.

This is my manifest.json :

{
    "name": "My Extension",
    "version": "0.0.1",
    "manifest_version": 2,
    "browser_action": {
        "default_icon": {
            "19": "icons/19x19.png",
            "38": "icons/38x38.png"
        },
        "default_title": "Quick Dashboard",
        "default_popup": "popup.html"
    },
    "permissions": [
       "http://www.domain.com/"
     ]
}

This is the popup.html :

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" src="jquery.js"></script> 
<script src="main.js"></script>
    <form id="url" method="post">
    <label for="url">A bar</label>
    <input id="urlname" name="url" type="text" value="" />
    <input type="submit" value="Send" />
    </form> 
    <div id="result"></div>


</div>
</body>
</html>

The ajax.php file:

if($_POST){

    die("success");

}

The actual problem with your code was incorrect host permission.

It should be (note the asterisk):

"permissions": [
   "http://www.domain.com/*"
 ]

That said, Jay S.'s answer works too.

In ajax.php, add

header("Access-Control-Allow-Origin : *")

To the top.

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