坚持CORS问题

时间:2015-08-25 18:12:42

标签: ajax cors

我看过很多关于在AJAX中请求'GET'时出现的'Access-Control-Allow-Origin'问题的StackOverflow问题。

我通过创建一个简单的PHP脚本来提供HTML文件(<?php include_once("home.html"); ?>),为Heroku部署了一个简单的静态应用程序。

的index.php

<?php header("Access-Control-Allow-Origin: *"); 
      header("Access-Control-Allow-Credentials: true "); 
      header("Access-Control-Allow-Methods: OPTIONS, GET, POST"); 
      header("Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control"); 
      include_once("home.html"); ?> 

home.html的

只是一个静态的html文件。 js文件:

$.ajax({ url: 'http://example.com', 
         type: 'GET', 
         crossDomain: true, 
         success: function(res) { 
                   console.log("hello"); 
                  } 
         })

我的网站使用AJAX提取图片。我已尽力使我的网站允许CORS,但没有任何方法可行。我尝试更改标题header("Access-Control-Allow-Origin: *");,甚至尝试编辑我的.htaccess文件。

我被困住了。我将不胜感激。

1 个答案:

答案 0 :(得分:1)

您无法同时使用凭据和*。如果要在请求中使用凭据,则需要在服务器上明确列出请求源。例如,Access-Control-Allow-Origin: example.com