前台自定义函数post请求接口

前台 vue-resource请求接口

addCustomer(e){
          if (!this.customer.name || !this.customer.phone || !this.customer.email) {
            //   console.log('请添加对应信息');
            this.alert = '请添加对应信息';
          }else{
              this.$http.post('http://localhost/phpcrud/app.php?action=create',{
                  "username":this.customer.name,
                  "phone":this.customer.phone,
                  "email":this.customer.email
              }).then(function(data){
                //   console.log(data);
                this.$router.push({
                    path:"/",
                    query:{
                        alert:"用户信息添加成功"
                    }
                });
                e.preventDefault();
              })
          }
          e.preventDefault();  
      }

后台php获取数据

if ($action == "create") {
    $input = file_get_contents("php://input");
    $json = json_decode($input);
    $username = $json->username;
    $email = $json->email;
    $phone = $json->phone;
    
    $conn->query("set names utf8");
    $result = $conn->query("insert into `users` (`username`,`email`,`phone`) values('$username','$email','$phone')");
    if($result){
        $res["message"] = "插入成功";
    }else{
        $res["message"] = "插入失败";
    }
}

猜你喜欢

转载自www.cnblogs.com/ayong6/p/12243217.html