这是两个页面,test.php和testserver.php.
test.php的
<script src="scripts/jq.js" type="text/javascript"></script>
<script>
$(function() {
$.ajax({url:"testserver.php",
success:function() {
alert("Success");
},
error:function() {
alert("Error");
},
dataType:"json",
type:"get"
}
)})
</script>
Run Code Online (Sandbox Code Playgroud)
testserver.php
<?php
$arr = array("element1",
"element2",
array("element31","element32"));
$arr['name'] = "response";
echo json_encode($arr);
?>
Run Code Online (Sandbox Code Playgroud)
现在我的问题是:当这两个文件都在同一台服务器(localhost或web服务器)上时,它可以工作并被alert("Success")调用; 如果它位于不同的服务器上,意味着Web服务器上的testserver.php和localhost上的test.php,它就无法工作,并且alert("Error")正在执行.即使ajax中的URL更改为http://domain.com/path/to/file/testserver.php
我正在研究我的这个个人项目,只是为了好玩,我想阅读一个xml文件,该文件位于http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml 并解析xml和用它来转换货币之间的价值.
到目前为止,我已经提出了下面的代码,这是非常基本的,以便读取xml,但我得到以下错误.
XMLHttpRequest无法加载****.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许来源" http://run.jsbin.com "访问.
$(document).ready(
function() {
$.ajax({
type: 'GET',
url: 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml',
dataType: 'xml',
success: function(xml){
alert('aaa');
}
});
}
);
Run Code Online (Sandbox Code Playgroud)
我没有看到我的代码有任何问题所以我希望有人可以指出我的代码错误,以及我如何解决它.