thinkphp5 ajax

关于thinkphp5中ajax的使用方法
html代码

<!DOCTYPE html>
<html>
	<head>
	<meta charset="UTF-8">
	<title>ajax</title>
	<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
	<script>
		function ajax(){
			var user = $("#user").val();
			var pass = $("#pass").val();
			$.ajax({
				type:"post",
				url:'login',
				data:{user:user,pass:pass},//这里data传递过去的是序列化以后的字符串
				success:function(data){
				$("#content").html(data.msg);//获取成功以后输出返回值
				}
			});
		}
	</script>
	</head>
	<body>
		<form action="" method="post" >
			<input  id="user" name="user" type="text"  autocomplete="off" >
			<input  id="pass" name="pass" type="text"  autocomplete="off">
			<button   type="button" onclick="ajax()" class="btn_login" >LOGIN</button>
		</form>
		<div id="content">
		</div>
	</body>
</html>

控制器代码

public function login($user,$pass){
		$where=[
			'user'=>$user,
		];
		$list=Db::name('user')->where($where)->find();
		if(empty($list)){
			$data['status'] = 0;
			$data['msg'] = "该用户不存在";
			return json($data);
		}else{
			$all=Db::name('user')->where('user',$user)->find();
			if(($all['pass'] == $pass)){ 
				$data['status'] = 1;
				$data['msg'] = "登录成功";
				return json($data);
			}else{
				$data['status'] = 0;
				$data['msg'] = "密码错误";
				return json($data);
				
			}
		}
	}

这是项目目录
在这里插入图片描述

发布了22 篇原创文章 · 获赞 2 · 访问量 439

猜你喜欢

转载自blog.csdn.net/jianchenn/article/details/104184419