0

我正在尝试制作一个简单的登录系统

我正在使用 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;
}  

因此,您可以看到电子邮件/密码没有随帖子一起发送,因为它们的值没有设置。

这里有什么问题?

4

1 回答 1

1

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

http://www.wireshark.org/

然后您可以读取服务器上的网络流量。

也许它甚至没有到达服务器。或者可能端口 8888 被阻止。

或者可能是 http 请求格式错误,服务器不知道该怎么做,所以它返回 400

对于您可以使用的过滤器

tpc.port == 8888

这样它只会显示端口 8888 上的 tcp 流量

于 2012-12-28T23:57:00.910 回答