ajax默认是不支持跨域操作的,只能提交跨域请求,无法获得响应。而可以用$.getScript()函数来进行跨域操作。代码如下:
<button id="btnTest">BtnTest</button>
<div id="result"></div>
$(document).ready(function(){
var oBtnTest = $("#btnTest");
oBtnTest.click(function(){
oBtnTest.disabled = true;
var oResult = $("#result");
oResult.html("loading").css("color","red");
jQuery.getScript("http://www.XXXX.com/Index/kuayu",
function(){
oResult.html("name:" + jimmy.name + "<br/>email:" + jimmy.email).css("color","black");
oBtnTest.disabled = false;
});
});
})
其中远程网址的响应内容为
public function kuayu(){
echo "var jimmy = {name:\"jimmy.yang\",email:\"[email protected]\"}";
}