我正在尝试使用 AFNetworking 框架将用户名和密码从 iOS 应用程序发送到 php 脚本。iOS 应用程序继续收到状态代码 401,我将其定义为“参数不足”。我尝试将 php 脚本中的“用户名”返回到 iOS 应用程序并接收 .
根据我迄今为止的调查,似乎是:
1) php 脚本未正确解码 POST 参数
2) iOS 应用未正确发送 POST 参数
以下是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;
}
我觉得我可能错过了什么。任何帮助都会很棒。