我整个星期都在处理一个问题,无法在任何地方找到答案。
这是问题所在:
XMLHttpRequest无法加载http://www.websiteA.com/process.php。 Access-Control-Allow-Origin不允许原点http://clientwebsi.te。
我有一个位于服务器的Javascript文件,来自网站A.客户端可以在那里的网站上加载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);
}
});
答案 0 :(得分:0)
$.ajax({
type: "POST",
url: "http://www.serverA.com/process.php",
dataType: "jsonp",
data: data,
crossDomain: true,
error: function(request,error){
alert(error);
},
success: function(request) {
alert(request.length);
}
答案 1 :(得分:-1)
唯一的方法是发出jsonp GET请求。这很容易,但你不能使用其他请求类型。
$.ajax({
dataType: 'jsonp',
url: 'http://domain.de/jsonp.php',
success: function(data, textStatus, jqXHR),
error: function(jqXHR, textStatus, errorThrown)
});