1

我试图根据这个答案实现跨域 AJAX 请求。

http://jsfiddle.net/PXSMQ/1/

我添加了以下标题:

header('Access-Control-Allow-Origin:  *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type');
header("Access-Control-Allow-Headers: x-requested-with");
header('Access-Control-Allow-Headers: x-csrf-token');

但我仍然收到此错误:

XMLHttpRequest cannot load http://www.belardi.ro/userTrack/addData.php. Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin. 
4

2 回答 2

3

Access Control必须在您将请求发送到的服务器上配置标头。你不能自己设置它们。如果您向其发送请求的服务器未正确配置,您将无法发出这些请求。

于 2013-06-19T10:06:37.053 回答
1

这些标头未出现在以下响应中addData.php

david@raston ~ $ curl -i -d 'some=json' http://www.belardi.ro/userTrack/addData.php
HTTP/1.1 200 OK
Date: Wed, 19 Jun 2013 10:12:35 GMT
Server: LiteSpeed
Connection: close
X-Powered-By: PHP/5.2.14
Content-Type: text/html
Content-Length: 16
Vary: User-Agent

Invalid page url~ :
于 2013-06-19T10:13:03.973 回答