jQuery AJAX跨域
这是两个页面,test.php和testserver.php。
test.php的
$(function() {
$.ajax({url:"testserver.php",
success:function() {
alert("Success");
},
error:function() {
alert("Error");
},
dataType:"json",
type:"get"
}
)})
testserver.php<?php
$arr = array("element1",
"element2",
array("element31","element32"));$arr['name'] = "response";echo json_encode($arr);?>
现在我的问题是:当这两个文件都在同一台服务器(localhost或web服务器)上时,它可以工作并被alert("Success")调用; 如果它位于不同的服务器上,意味着Web服务器上的testserver.php和localhost上的test.php,它就无法工作,并且alert("Error")正在执行。即使ajax中的URL更改为http://domain.com/path/to/file/testserver.php