Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 );
    });
});
like image 988
Canvas Avatar asked Sep 16 '25 06:09

Canvas


2 Answers

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).

like image 166
Quentin Avatar answered Sep 17 '25 20:09

Quentin


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", "*")

like image 40
Aaron Avatar answered Sep 17 '25 19:09

Aaron