Ajax初级 json_encode
Ajax:Asynchronous JavaScript and XML Ajax用来异步通信,用户不需要长长的等待。 实现也很简单,主要用到XMLHttpRequest对象 HTML代码: <body> <form> </body> <script> var
XMLHttp=
null
;//兼容多种浏览器
try
{
new
ActiveXObject(
"Msxml2.XMLHTTP"
);
}
catch
(e){
{
"Microsoft.XMLHTTP"
);
}
}
if
(XMLHttp==
){
XMLHttpRequest();
}
function callServer(){
var city=document.getElementById("city").value; var state=document.getElementById("state").value; if((city==null)||(city=="")) return; if((state==null)||(state=="")) return; var url="__APP__/Index/test2?city="+escape(city)+"&state="+escape(state);//这里用的thinkphp框架,一般是xx/yy/z.php xmlHttp.open("GET",url,true); xmlHttp.onreadystatechange=updatePage; xmlHttp.send(null); }
function updatePage(){
if (xmlHttp.readyState==4) { if (xmlHttp.status==200){ var response=xmlHttp.responseText; document.getElementById("zipCode").value=response;
}
}
</script>
后台PHP: public function test2(){ //获得来自 URL 的参数 $c=$_GET["city"]; } 后台处理后的数据放到$response(这只是随便取的变量名,可以改的)里 然后前台的updatePage函数通过xmlHttp.responseText获取,就行啦~ 如果要传数组,就要用json格式,比如: $response=("1","2","3"); echo json_encode($response); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |