1.$.get
$.get("aaaa.php", { action:"get",name:"lulu" }, function(json){
alert(json.info);
});
2.$.post
$.post("aaaa.php", { action:"get",name:"lulu" }, function(json){
alert(json.info);
});
3.$.ajax
$.ajax(
{
type : "post",
url : "www.xxxx.com",
success : function (json)
{
if (json.status == 1) {
alert(json.info);
}
}
});
以$.post作为一个例子:
前端代码(在js的dologin()函数用post提交,取得返回值进行判断):
<form action="#" method="post" >
<div >
<div style="line-height: 30px"> </div>
<div style="padding: 0px 280px";><font color="red">*</font>用户名:<input type="text" name="username" id="username" onkeydown="EnterPress(event)" ></div><div style="margin: 7px 100px;"><img src="__PUBLIC__/Index/images/xian.png" width="650px" /></div>
input type="hidden" name="password" id="password" value="password">
<div style="padding: 0px 292px";><font color="red">*</font>密码:<input id="pwd" type="password" onBlur="chana()" onkeydown="EnterPress(event)" ></div>
<div style="margin: 7px 100px;"><img src="__PUBLIC__/Index/images/xian.png" width="650px" /></div>
<div style="padding: 3px 280px;"><font color="red">*</font>验证码:<input type="text" name="vcode" id="vcode" style="width:100px" onkeydown="EnterPress(event)" ><a href="javascript:void(0);" style="color:#110000" onclick="change(imagecode)" ><font size='3px'> 换一个</font></a></div>
<div style="padding: 3px 348px;"><font size="2px">请输入动画图片中的字符</font></div>
<img style="padding: 3px 348px;" src='__APP__/Index/Index/verify/' id="imagecode" onclick="change(this)" />
<div style="margin: 7px 100px;"><img src="__PUBLIC__/Index/images/xian.png" width="650px" /></div>
<div style="margin: 0px 340px;"><input type="checkbox" name="auto" value="1" /><font size="2px">自动登录</font></div>
<div style="margin: 25px 314px;" ><img name="submit" src="__PUBLIC__/Index/images/login2.png" style="width:80px" onclick="dologin()"/><span style="white-space:pre"> </span>function change(obj){
<span style="white-space:pre"> </span>obj.src='__APP__/Index/Index/verify/'+Math.random();
}</div>
</div>
</form>
<div style="width: 900px;margin:auto;position: absolute;top:641px;left:63px;" class="buttomimage"></div>
<script>
function dologin(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
var vcode=document.getElementById("vcode").value;
$.post("<{:U('Index/Index/do_login')}>", {'username':username,'password':password,'vcode':vcode},
function(json){
if(json.status) window.location.href="__ROOT__/index.php/Index/Index/index";
else alert("登录失败,验证码或密码错误");
});
} function change(obj){
<span style="white-space:pre"> </span>obj.src='__APP__/Index/Index/verify/'+Math.random();
}
</script>
控制层代码(用ajaxReturn返回数据):
public function do_login(){
$password=I('post.password');
//验证用户和密码是否正确
if(!IS_POST){
//_404("非法访问");
//halt()
$this->error('非法访问',U('index','',''));
}
if(I('post.vcode','','md5')!=$_SESSION[C('SESSION_PREFIX')]['verify']){
$this->error('验证码输入错误',U('Index/Index/login'));
}
$username=I('post.username');
$user=M('user');
$res=$user->where(array( 'username'=>$username,'password'=>$password))->find();
// $user->getLastsql();
if(is_null($res)){
$this->ajaxReturn('', '登录失败', 0);
}else{
//如果正确写入SESSION;
session('nameuser',$username);
$user=M('user');
$data['ip']=getIP();
$user->where(array('username'=>$username))->save($data);
$this->ajaxReturn('', '登录成功', 1);
}
}
注:url皆为控制层处理函数的地址,在函数可以用$this->ajaxReturn($data,$infor,$status)返回数据,在Firefox用:查看元素->控制台->网络,可查看提交的信息和返回的数据,方便调试。
参考链接:http://www.thinkphp.cn/topic/8988.html