AFNetworking PostPath php Parameters are null



我正在尝试使用afnetworking框架从iOS应用程序发送一个用户名和密码到PHP脚本。iOS应用程序继续接收状态代码401,我将其定义为"没有足够的参数"。我尝试将"用户名"从php脚本返回到iOS应用程序并接收。

基于我到目前为止一直在调查的内容,似乎是:

1)PHP脚本未正确解码post参数

2)iOS应用程序未正确发送邮政参数

以下是iOS函数

- (IBAction)startLoginProcess:(id)sender
{
NSString *usernameField = usernameTextField.text;
NSString *passwordField = passwordTextField.text;
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:usernameField,        @"username", passwordField, @"password", nil];
NSURL *url = [NSURL URLWithString:@"http://localhost/~alejandroe1790/edella_admin/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient defaultValueForHeader:@"Accept"];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient postPath:@"login.php" parameters:parameters
             success:^(AFHTTPRequestOperation *operation, id response) {
                 NSLog(@"operation hasAcceptableStatusCode: %d", [operation.response statusCode]);
             }
             failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 NSLog(@"Error with request");
                 NSLog(@"%@",[error localizedDescription]);
             }];
}

以下是PHP脚本

function checkLogin()
{
    // Check for required parameters
    if (isset($_POST["username"]) && isset($_POST["password"]))
    {
        //Put parameters into local variables
        $username = $_POST["username"];
        $password = $_POST["password"];
        $stmt = $this->db->prepare("SELECT Password FROM Admin WHERE Username=?");
        $stmt->bind_param('s', $username);
        $stmt->execute();
        $stmt->bind_result($resultpassword);
        while ($stmt->fetch()) {
            break;
        }
        $stmt->close();
        // Username or password invalid
        if ($password == $resultpassword) {
            sendResponse(100, 'Login successful');
            return true;
        }
        else 
        {
            sendResponse(400, 'Invalid Username or Password');
            return false;
        }
    }
    sendResponse(401, 'Not enough parameters');
    return false;
}

我觉得我可能缺少一些东西。任何帮助都很棒。

问题是您将编码参数编码为JSON,并且通过AFHTTPClient类将其设置为HTTP主体。您可以通过不将编码设置为JSON来解决此问题并使用$_POST

- (IBAction)startLoginProcess:(id)sender
{
NSString *usernameField = usernameTextField.text;
NSString *passwordField = passwordTextField.text;
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:usernameField,        @"username", passwordField, @"password", nil];
NSURL *url = [NSURL URLWithString:@"http://localhost/~alejandroe1790/edella_admin/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient defaultValueForHeader:@"Accept"];
[httpClient postPath:@"login.php" parameters:parameters
             success:^(AFHTTPRequestOperation *operation, id response) {
                 NSLog(@"operation hasAcceptableStatusCode: %d", [operation.response statusCode]);
             }
             failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 NSLog(@"Error with request");
                 NSLog(@"%@",[error localizedDescription]);
             }];
}

但是,如果选择,您可以将参数发送为JSON字符串,但是您将无法使用$_POST,并且需要json_decode $HTTP_RAW_POST_DATA

function checkLogin()
{
    global $HTTP_RAW_POST_DATA;
    // remove the second argument or pass false if you want to use an object
    $user_info = json_decode($HTTP_RAW_POST_DATA, true);
    // Check for required parameters
    if (isset($user_info["username"]) && isset($user_info["password"]))
    {
        //Put parameters into local variables
        $username = $user_info["username"];
        $password = $user_info["password"];
        $stmt = $this->db->prepare("SELECT Password FROM Admin WHERE Username=?");
        $stmt->bind_param('s', $username);
        $stmt->execute();
        $stmt->bind_result($resultpassword);
        while ($stmt->fetch()) {
            break;
        }
        $stmt->close();
        // Username or password invalid
        if ($password == $resultpassword) {
            sendResponse(100, 'Login successful');
            return true;
        }
        else 
        {
            sendResponse(400, 'Invalid Username or Password');
            return false;
        }
    }
    sendResponse(401, 'Not enough parameters');
    return false;
}

最新更新