【发布时间】:2012-06-15 22:50:18
【问题描述】:
我整整一周都在处理一个问题,但在任何地方都找不到答案。
这就是问题所在:
XMLHttpRequest 无法加载 http://www.websiteA.com/process.php。 Access-Control-Allow-Origin 不允许来源http://clientwebsi.te。
我有一个位于网站 A 的服务器上的 Javascript 文件。客户端可以在该网站上加载 JS 文件。
在服务器 A 上还有一个 process.php,用于将信息放入服务器 A 的数据库中。
我现在正在使用这个代码:
var dataText = 'page=' + top.location.host;
$.ajax({
type: "POST",
url: "process.php",
data: dataText,
error: function(request,error){
alert(error);
},
success: function(request) {
alert(request.length);
}
});
此代码在 localhost 上完美运行,但当我使用服务器 A 和客户端服务器(跨域)时却不行
这是在线代码:
$.ajax({
type: "POST",
url: "http://www.serverA.com/process.php",
dataType: "json",
data: dataText,
error: function(request,error){
alert(error);
},
success: function(request) {
alert(request.length);
}
});
【问题讨论】:
-
抱歉,无法按照您想要的方式完成。跨站点脚本是一个很大的禁忌。您需要使用 JSONP。
标签: php ajax json cross-domain