服务器端如何向客户端发送数据?
<br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><br /><html><br /> <head><br /> <meta http-equiv="conent-type" content="text/html; charset=utf-8"><br /> <title> client post</title><br /> <script src="//code.jquery.com/jquery-1.11.0.min.js"></script><br /> </head><br /><br /> <body><br /> <script type="text/javascript"><br /> function fsubmit(){<br /> $.post("server.php", { username: $("#username").val(), password: $("#password").val() },function(ret){<br /> if(ret.success==true){<br /> alert('login success');<br /> }else{<br /> alert('login fail');<br /> }<br /> },'json');<br /> }<br /> </script><br /> <p>username:<input type="text" id="username"></p><br /> <p>password:<input type="text" id="password"></p><br /> <p><input type="button" value="submit" onclick="fsubmit()"></p><br /> <br /> </body><br /></html><br />
<br /><?php<br />$username = isset($_POST['username'])? $_POST['username'] : '';<br />$password = isset($_POST['password'])? $_POST['password'] : '';<br /><br />$ret = array();<br /><br />if($username=='fdipzone' && $password=='123456'){<br /> $ret['success'] = true;<br />}else{<br /> $ret['success'] = false;<br />}<br /><br />echo json_encode($ret);<br />?><br />
<br /><!DOCTYPE html><br /><html><br /><head><br /> <title></title><br /> <script<br /> src="http://code.jquery.com/jquery-latest.js"></script><br /></head><br /><body><br /><br /><script><br /> $(document).ready(function () {<br /> $.post('test.php?gettest=1', {"test": "test"}, function (data) {<br /> alert(data.get);<br /> alert(data.post);<br /> }, 'json')<br /> })<br /></script><br /></body><br /></html><br />
<br />$array = array(<br /> 'get' => $_GET['gettest'],<br /> 'post' => $_POST['test'],<br />);<br /><br />echo json_encode($array);<br />