2012-12-28 75 views
0

我试图做一个简单的登录系统AFNetworking返回400

我使用AFNetworking,这是我的登录功能,因为它代表:

- (void) login { 

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:email.text, @"email", password.text, @"password", nil]; 

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:8888"]]; 
[httpClient setParameterEncoding:AFFormURLParameterEncoding]; 

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" 
                 path:@"http://localhost:8888/api.php" 
                parameters:params]; 


AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSString *response = [operation responseString]; 
    NSLog(@"response: [%@]",response); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"error: %@", [operation error]); 
}]; 

[operation start]; 

}

它返回状态400 “坏请求”,所以我假设我的帖子格式不正确。但我似乎无法找到它有什么问题?

这里是在登录APi的

function login() { 

    // Check for required params 
    if (isset($_POST['email']) && isset($_POST['password'])) { 

     // Put params into local variables 
     $email = $_POST['email']; 
     $pass = $_POST['password']; 

     echo 'Your email:' + $email + 'and password: ' + $password; 

     // look up params in db 
     $stmt = $this->db->prepare('SELECT `email`, `password`, `group_name` FROM `User` WHERE `email` = ? AND `password` = ?'); 
     $stmt->bind_param('ss', $email, $password); 
     $stmt->execute(); 
     $stmt->bind_result($u_email, $u_password, $group_name); 
     while ($stmt->fetch()) { 
      break; 
     } 

     $stmt->close(); 

     // Bail if code doesn't exist 

     // Return unlock code, encoded with JSON 
     $result = array(
      "group_name" => $group_name, 
     ); 

     sendResponse(200, json_encode($result)); 
     return true; 
    } 

    sendResponse(400, 'Invalid request'); 
    return false; 
} 

因此,大家可以看到电子邮件/密码不被用邮寄给他们的价值观没有设置一目了然。

这里有什么问题?

回答

1

在运行Web主机的机器上获取wireshark。

http://www.wireshark.org/

,那么你可以阅读来在服务器上的网络流量。或许它甚至没有进入服务器。或者也许8888端口被阻塞。

或许http请求的格式不正确并且服务器不知道该怎么办所以它返回一个400

的过滤器,你可以使用

tpc.port == 8888 

这样它只会显示TCP流量在端口8888

+0

谢谢我认为这可能是问题。 – codeRefiner