简体   繁体   中英

Chrome Origin null is not allowed by Access-Control-Allow-Origin

I have ready many of the other posts ppl have asked here about the origin not allowed blablabla,

Now I have tried to add --Access-Control-Allow-Origin and enable apps and even disabled security but every time I try my button on the php page it just keeps stating

Origin null is not allowed by Access-Control-Allow-Origin.

Can anyone help me at all? Here is the code that is causing the problem

page.php

<html land="en">
<head>
    <meta carset="utf-8">
    <link rel="stylesheet" type="text/css" href="style.css";
</head>

<body>
<!-- Document Ready Event -->
<input id="text" type="text" /><input id="submit" type="button" value="Submit" />

<div id="feedback"></div>
    <script src="../jquery-1.10.2.min.js"></script>
    <script src="script.js"></script>
</body>
</html>

Script.js

$('#submit').click( function()
{
var text = $('#text').val();

$.get( 'PHP/reverse.php', { input: text }, function( data )
    {
        $('#feedback').text( data );
    });
});

Origin null is not allowed by Access-Control-Allow-Origin means that you are trying to perform Ajax on a local file. This is forbidden for security reasons. Even if this was not the case, your PHP wouldn't run because PHP is supported by web servers, not web browsers.

You have a web server installed. You have to request your pages through the server, rather than accessing them directly from your file system.

Use a URL starting with http://localhost/

You will need to move your files so that they are under the server's DocumentRoot (or reconfigure the server so that it can access them from their present location).

I'm not sure which browser you are testing for. In my case, it works for all my browsers if I sent AJAX calls from local file, which means the Origin is null . I guess the reason you can not get it work is that some server side coding are needed.

Try this, add the Access-Control-Allow-Origin header to the HTTP response and set the value to * . I'm not sure how to do this in PHP, but here's a piece of Java code for your information.

response.setHeader("Access-Control-Allow-Origin", "*")

There is this chrome plugin allows to request any site with ajax from any source. Adds to response 'Allow-Control-Allow-Origin: *' header

https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi/related

It worked for me without needing to change anything on the server side. But you should change withCredentials: false in the request, if it conforms with your tests.

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