alamofire发送post请求时缺少参数响应

tkclm6bt  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(315)

我正在尝试使用alamofire连接到mysql数据库进行用户身份验证。
这是我的登录按钮操作方法中的代码

//getting the username and password
    let params: Parameters = ["username":username.text!,"password":password.text!]

    //making a post request
    Alamofire.request(URL_USER_LOGIN, method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON
        {
            response in
            //printing response
            print(response)

            //getting the json value from the server
            if let result = response.result.value {
                let jsonData = result as! NSDictionary

                //if there is no error
                if(!(jsonData.value(forKey: "error") as! Bool)){

                    //getting the user from response
                    let user = jsonData.value(forKey: "user") as! NSDictionary

                    //getting user values
                    let userId = user.value(forKey: "id") as! Int

                    //saving user values to defaults
                    self.defaultValues.set(userId, forKey: "userid")

                    //switching the screen
                    let TableView = self.storyboard?.instantiateViewController(withIdentifier: "TableView") as! TableView
                    self.navigationController?.pushViewController(TableView, animated: true)

                    self.dismiss(animated: false, completion: nil)
                }else{
                    //error message in case of invalid credential
                    self.labelMessage.text = "Invalid username or password"
                }
            }
    }

当我收到服务器的响应时

SUCCESS: {
    error = 1;
    message = "Parameters are missing";
}

我也尝试过使用不同类型的编码,比如urlencoding.default,但是出现了这个错误
失败:responseserializationfailed(alamofire.aferror.responseserializationfailurereason.jsonserializationfailed(error domain=nscocaerorDomain code=3840“字符0周围的值无效。”userinfo={nsdebugdescription=字符0周围的值无效。}))
这就是我的login.php的样子

<?php

    require_once 'dboperation.php';

    $response = array();

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {

        if (isset($_POST['username']) && isset($_POST['password'])) {

            $db = new dboperation();

            if ($db->userLogin($_POST['username'], $_POST['password'])) {
                $response['error'] = false;
                $response['user'] = $db->getUserByUsername($_POST['username']);
            } else {
                $response['error'] = true;
                $response['message'] = 'Invalid username or password';
            }

        } else {
            $response['error'] = true;
            $response['message'] = 'Parameters are missing';
        }

    } else {
        $response['error'] = true;
        $response['message'] = "Request not allowed";
    }

    echo json_encode($response);
    ?>

数据库操作.php

<?php
class dboperation

{
    private $conn;

    function __construct()
    {
        require_once 'Constants.php';
        require_once 'dbconnect.php';
        // opening db connection
        $db = new DbConnect();
        $this->conn = $db->connect();
    }

    /*
     * This method is added
     * We are taking username and password
     * and then verifying it from the database
     * */

    public function userLogin($username, $pass)
    {
        $stmt = $this->conn->prepare("SELECT idx, user_Id
                                    FROM Employee
                                    WHERE user_Id = ?
                                    AND user_Password = ?
                                    AND deleteYn = 'N'");
        $stmt->bind_param("ss", $username, $pass);
        $stmt->execute();
        $stmt->store_result();
        return $stmt->num_rows > 0;
    }

    /*
     * After the successful login we will call this method
     * this method will return the user data in an array
     * */

    public function getUserByUsername($username)
    {
        $stmt = $this->conn->prepare("SELECT user_Id FROM Employee WHERE user_Id = ? AND deleteYn = 'N'");
        $stmt->bind_param("s", $username);
        $stmt->execute();
        $stmt->bind_result($id);
        $stmt->fetch();
        $user = array();
        $user['id'] = $id;
        return $user;
    }

    private function isUserExist($username)
    {
        $stmt = $this->conn->prepare("SELECT user_Id FROM Employee WHERE user_Id = ? AND deleteYn = 'N'");
        $stmt->bind_param("sss", $username);
        $stmt->execute();
        $stmt->store_result();
        return $stmt->num_rows > 0;
    }

}
?>

数据库连接.php

<?php

class DbConnect
{
    private $conn;

    function __construct()
    {
    }

    /**
     * Establishing database connection
     * @return database connection handler
     */
    function connect()
    {
        require_once 'Constants.php';

        // Connecting to mysql database
        $this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

        // Check for database connection error
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        // returing connection resource
        return $this->conn;
    }
}
?>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题