Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:前端的交互数据, 很有用
向指定的资源提交要处理的数据$.post( url, [ data ], [ success ], [ dataType ] )
第一个参数是请求到哪个页面,第二个参数是传递的数据,第三个是请求成功后怎么处理后台返回的数据,以什么格式处理后台返回的数据。
function里面的res随便写
var _username = $.trim($('input[name="username"]').val());
var _pwd = $.trim($('input[name="pwd"]').val());
$.post('/dologin.php',{username:_username,pwd:_pwd},function(res){
alert(res.msg);
if(res.code==0){
//window.location.href = "/home.html";
window.location.reload();
}
},'json');
$username = $_POST['username'];
$pwd = $_POST['pwd'];
if($username!='admin'){
$data = json_encode(array('code'=>1,'msg'=>'用户名错误'));
exit($data);
}
if($pwd!='123456'){
$data = json_encode(array('code'=>1,'msg'=>'password error!'));
exit($data);
}
$data = json_encode(array('code'=>0,'msg'=>'登录成功'));
exit($data);
从指定的资源请求数据
function city() {
$.get('getcity.php',function (re) {
var html = '';
$.each(re.data,function (i,v) {
console.log(v);
html += '<option value="'+v.id+'">'+v.title+'</option>';
});
$('select[name="city"]').html(html);
},'json');
}
<?php
$citys = array(
array('id'=>3,'title'=>'合肥'),
array('id'=>6,'title'=>'黄山'),
array('id'=>7,'title'=>'安庆')
);
$data = json_encode(array('code'=>0,'msg'=>'success','data'=>$citys));
exit($data);
将用作提交的表单元素的值编译成字符串<br />把表单里面的可以提交的数据全部序列化提交
$.post('/dologin.php',$('form').serialize(),function(res){
alert(res.msg);