doucanrui1735 2015-04-18 18:51
浏览 54

即使标题存在,CORS也会失败

I am using a CORS request to post data to a php script on a different domain. I am getting the error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at 'mydomain'(Reason: CORS header 'Access-Control-Allow-Origin' missing).

I have already set the headers in php using the following code.

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: OPTIONS, GET, POST");

I have also tried the code in this post

This is the javascript code for reference

$.ajax(url, {
    type:"POST",
    dataType:"json",

    data: {
        name:"something"
    },

    success:function(data, textStatus, jqXHR) {
        alert("success");
    },

    error: function(jqXHR, textStatus, errorThrown) {
        alert("failure");
    }
});

Response Headers

Age:1
Cache-Control:max-age=900
Connection:keep-alive
Content-Length:356
Content-Type:text/html; charset=utf-8
Date:Sun, 19 Apr 2015 04:26:27 GMT
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET

Request Header

Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:14
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
DNT:1
Host:www.jurney.co
Origin:http://isabelinc.in
Pragma:no-cache
Referer:http://isabelinc.in/jurney/testcors.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36 

But I've had no luck. Any help is appreciated

  • 写回答

1条回答 默认 最新

  • dsegw3424 2016-08-04 10:05
    关注

    Try this:

    <?php
        // Allow from any origin
        if (isset($_SERVER['HTTP_ORIGIN'])) {
            header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
            header('Access-Control-Allow-Credentials: true');
            header('Access-Control-Max-Age: 86400');    // cache for 1 day
        }
    
        // Access-Control headers are received during OPTIONS requests
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    
            if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
                header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         
    
            if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
                header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    
            exit(0);
        }
    
        echo "You have CORS!";
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)