21,886
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="conent-type" content="text/html; charset=utf-8">
<title> client post</title>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<script type="text/javascript">
function fsubmit(){
$.post("server.php", { username: $("#username").val(), password: $("#password").val() },function(ret){
if(ret.success==true){
alert('login success');
}else{
alert('login fail');
}
},'json');
}
</script>
<p>username:<input type="text" id="username"></p>
<p>password:<input type="text" id="password"></p>
<p><input type="button" value="submit" onclick="fsubmit()"></p>
</body>
</html>
server.php
<?php
$username = isset($_POST['username'])? $_POST['username'] : '';
$password = isset($_POST['password'])? $_POST['password'] : '';
$ret = array();
if($username=='fdipzone' && $password=='123456'){
$ret['success'] = true;
}else{
$ret['success'] = false;
}
echo json_encode($ret);
?>
<script type="text/javascript">
function submits(){
// 实现账户与密码向服务器的发送
// 服务器地址中传递的有参数a=1
// 从服务器端接收数据num
// 对num进行乘以2运算,再将结果发送给服务器端
}
</script>
<form id="loginform" action="./login.php?a=1" method="post" >
username<input type="text" id="username" name="account">
password<input type="password" id="pwd" name="password" >
<input type="button" onclick="submits();">
</form>
<?php
if($_POST['a'] == 1){
$uName = $_POST['account'];
$uPassword = $_POST['password'];
// 比较数据库中信息(省略)
// 向客户端传递数据num
// 获得客户端发送来的运算结果
}
?>
就是为了实现客户端先向服务器发送数据,服务器再向客户端响应数据,最后客户端向服务器端发送对响应数据的运算结果。
总共是三轮数据交换过程,这个该怎么实现呢?
<!DOCTYPE html>
<html>
<head>
<title></title>
<script
src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<script>
$(document).ready(function () {
$.post('test.php?gettest=1', {"test": "test"}, function (data) {
alert(data.get);
alert(data.post);
}, 'json')
})
</script>
</body>
</html>
$array = array(
'get' => $_GET['gettest'],
'post' => $_POST['test'],
);
echo json_encode($array);