【发布时间】:2012-07-26 16:16:13
【问题描述】:
可以把完整的 URL 路径放在 ajax 中吗?我在访问 url 时遇到问题,我的错误响应状态为 0。
$.ajax({
url: "http://fullurlpath.com/php/myphppagedata.php",
type: "GET",
data: "somedata="+somedata,
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
}).error(function(xhr){
alert(xhr.responseText);
alert(xhr.status);
}).done(function(data){
alert(data);
});
另外,在我的 http://fullurlpath.com/php/myphppagedata.php 我有
header('Access-Control-Allow-Origin: *');
【问题讨论】:
-
@Musa,我尝试使用相对路径,但我收到 404 错误,我假设它没有获取 URL。这让我很困惑,因为我在 js 文件夹中有这组代码,而
myphppagedata.php在 php 文件夹中。所以我会做../php/myphppagedata.php -
@ExplosionPills 我正在通过不同的域访问它,这就是我尝试使用完整路径的原因。但我刚刚获得状态 0
-
另外,我要添加
$.support.cors = true; -
您不能对域以外的域进行 Ajax 调用,浏览器会阻止这些调用。有一些变通方法,比如 jsonp(jQuery 支持)。
标签: php jquery ajax header http-headers