jQuery AJAX跨域這是兩個頁面,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>testserver.php<?php
$arr = array("element1",
"element2",
array("element31","element32"));$arr['name'] = "response";echo json_encode($arr);?>現(xiàn)在我的問題是:當(dāng)這兩個文件都在同一臺服務(wù)器(localhost或web服務(wù)器)上時,它可以工作并被alert("Success")調(diào)用; 如果它位于不同的服務(wù)器上,意味著Web服務(wù)器上的testserver.php和localhost上的test.php,它就無法工作,并且alert("Error")正在執(zhí)行。即使ajax中的URL更改為http://domain.com/path/to/file/testserver.php
jQuery AJAX跨域
回首憶惘然
2019-05-24 14:39:12