PHP-AJAX CORS Fails due to Access-Control-Allow-Origin

我怕爱的太早我们不能终老 提交于 2019-12-08 05:00:53

问题


I am trying to make an AJAX call (CORS) using the below code:

$.ajax({
    type: "POST",
    url: 'http://localhost/MySpace',
    success: function(result) {
         console.log(result);
    },
    error: function() {
         console.log("error");
    },
});

I am running the above code from:

http://127.0.0.1/Test/index.html

The PHP Code written at http://localhost/MySpace is as below:

<?php
    header("Access-Control-Allow-Origin: *");
    echo "Hello";
?>

As per my understanding, this should have worked. However I am getting this error:

XMLHttpRequest cannot load http://localhost/MySpace. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1' is therefore not allowed access.

What should I do make this work? Or am I doing something entirely wrong?

As per suggestions to debug the request I tried making a curl request:

curl -i http://127.0.0.1/MySpace/

And in response I can see that Access-Control-Allow-Origin is marked as *:

HTTP/1.1 200 OK
Date: Fri, 13 May 2016 05:59:19 GMT
Server: Apache/2.4.18 (Unix) OpenSSL/1.0.2g PHP/5.6.19 mod_perl/2.0.8-dev Perl/v5.16.3
X-Powered-By: PHP/5.6.19
Access-Control-Allow-Origin: *
Content-Length: 5
Content-Type: text/html; charset=UTF-8

As per the comment I added the below code to my .htaccess:

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

@Edit:

This is my Response Header:

HTTP/1.1 301 Moved Permanently
Date: Fri, 13 May 2016 09:26:44 GMT
Server: Apache/2.4.18 (Unix) OpenSSL/1.0.2g PHP/5.6.19 mod_perl/2.0.8- dev Perl/v5.16.3
Location: http://localhost/elasticservice/
Content-Length: 240
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1

This is my request header:

POST /elasticservice HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 0
Cache-Control: max-age=0
Accept: */*
Origin: http://127.0.0.1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
Referer: http://127.0.0.1/test/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,hi;q=0.6

回答1:


You have allowed CORS Origin, so for access cross domain 3 headers (Origin, Methods, Headers) compulsory, see below sample headers

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");

@update: you can try this solution

header('Access-Control-Allow-Origin: *');
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
            $headers=getallheaders();
            @$ACRH=$headers["Access-Control-Request-Headers"];
            header("Access-Control-Allow-Headers: $ACRH");
        }

header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");



回答2:


try the following url:

$.ajax({
    type: "POST",
    url: 'http://127.0.0.1/MySpace',
    success: function(result) {
         console.log(result);
    },
    error: function() {
         console.log("error");
    },
});


来源:https://stackoverflow.com/questions/37204157/php-ajax-cors-fails-due-to-access-control-allow-origin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!