<?php
namespace app\index\controller;
use think\Controller;
header("Content-type: text/html; charset=utf-8");
class Index extends Controller
{
public function index()
{
return $this->fetch();
}
function send_msg($status,$msg){
$res["status"]=$status;
$res["msg"]=$msg;
echo json_encode($res);
exit;
}
function login(){
$user = $_POST["user"];
$password = $_POST["pwd"];
if (empty($user)){
$this->send_msg(-1,"请输入账号");
}
if (empty($pwd)){
$this->send_msg(-1,"请输入密码");
}
$where["name"]=$user;
$info = DB("user")
->where($where)
->find();
if (empty($info)){
$this->send_msg(-1,"用户xxx");
}
if ($info["password"]!=$password){
$this->send_msg(-1,"密码xx");
}
$this->send_msg(1,"完美");
echo "okokok";
}
}
//js代码
function login(){
var user = $("#user").val();
var pwd = $("#pwd").val();
var post_data = {user:user,pwd:pwd};
var data = {url:"/index.php/index/Index/login",
data:post_data,
type:"post",
dataType:"json",
success:function(res){
if(res.status == -1){
alert(res.msg);
}
if(res.status == 1){
alert(res.msg);
}
}};
$.ajax(data);
}
var post_data = {user:user,pwd:pwd};
这么写不对吧
var post_data = {"user":user,"pwd":pwd};
看看
send_msg这个方法有问题吧
还有看看是否有错误
检查一下是不是这里的问题 -> parseInt(res.status)
给强转一下int试试
开发过程中碰到这种问题 你应该先看一下是不是js报错了,如果不是js的问题,那么就看一下ajax的请求地址是不是出错了,在F12的network里点你的请求地址,能看到返回的结果,可以在这个接口里输出一下前台传过来的数据,看看是不是能成功接到
ajax你确定语法对了嘛?你要请求路径喝请求的参数是否正确设置
可以alert()下 一步一步测试看哪块出了问题
首先你写的代码接受参数存在问题,不应该使用$_POST因为这个可能使存在注入点,其次 你可以在send_msg,echo 前 加入ob_clean防止其他输出内容扰乱页面正常输出,最后确认返回码是不是200