跨域AJAX不工作PHP

Cross-domain AJAX not working PHP

本文关键字:PHP 工作 AJAX 跨域      更新时间:2023-09-26

我已经尝试实现一个跨域的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. 

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

这些标头没有出现在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~ :