使用带有RestKit的POST登录会返回404错误

时间:2013-10-06 04:38:58

标签: iphone ios objective-c post restkit

我是RestKit的新手,我正在尝试使用POST请求登录我的系统。我正在使用RestKit版本0.20.3,这就是我的做法:

- (IBAction)login:(id)sender {
    NSString *email = [self.emailTextField text];
    NSString *password = [self.passwordTextField text];

    NSURL *url = [[NSURL alloc] initWithString:@"http://myhost.com/api.php"];
    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:url];
    NSDictionary *tmp = @{@"rquest":@"user",
                          @"tag":@"login",
                          @"email":email,
                          @"password":password};
    [manager postObject:tmp path:@"" parameters:nil
                success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                    NSDictionary *result = [mappingResult dictionary];
                    if([[result objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithInt:1]]){
                        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
                        [def setBool:YES forKey:@"isLoggedIn"];
                        // set user details...
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    }
                }
                failure:^(RKObjectRequestOperation *operation, NSError *error) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                    message:[error localizedDescription]
                                                                   delegate:nil
                                                          cancelButtonTitle:@"OK"
                                                          otherButtonTitles:nil];
                    [alert show];
                    NSLog(@"Hit error: %@", error);
                }];
}

如您所见,由于我不需要将响应映射到对象,因此我尝试使用NSDictionary访问响应数据。我不确定这是不是问题,但是当我尝试运行上面的代码时,我收到错误:

013-10-06 11:24:51.897 Eateries[1182:454b] E restkit.network:RKResponseMapperOperation.m:304 Failed to parse response data: Loaded an unprocessable response (404) with content type 'application/json'
2013-10-06 11:24:51.902 Eateries[1182:1003] E restkit.network:RKObjectRequestOperation.m:243 POST 'http://myhost.com/api.php' (404 Not Found / 0 objects) [request=0.1479s mapping=0.0000s total=0.1648s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (404) with content type 'application/json'" UserInfo=0x8ee81e0 {NSErrorFailingURLKey=http://myhost.com/api.php, NSUnderlyingError=0x8ef4ea0 "The operation couldn’t be completed. (Cocoa error 3840.)", NSLocalizedDescription=Loaded an unprocessable response (404) with content type 'application/json'}
2013-10-06 11:24:51.978 Eateries[1182:a0b] Hit error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (404) with content type 'application/json'" UserInfo=0x8ee81e0 {NSErrorFailingURLKey=http://myhost.com/api.php, NSUnderlyingError=0x8ef4ea0 "The operation couldn’t be completed. (Cocoa error 3840.)", NSLocalizedDescription=Loaded an unprocessable response (404) with content type 'application/json'}

我真的很困惑,因为我真的不知道我在这里做错了什么。如果您有任何建议,请告诉我。谢谢。

P.s:我为了个人目的更改了主机的名称,但是当我尝试从其他平台测试时,我的服务器确实对该请求做出了响应。

1 个答案:

答案 0 :(得分:0)

您使用的是错误的API。

postObject:...用于发布要映射的对象,这意味着object参数(如果不是nil)将用作映射响应的目标。

如果您不想映射响应,只需使用基础AFHTTPClient执行普通POST请求。

[[RKObjectManager sharedManager].HTTPClient postPath:@"" parameters:tmp success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // ...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // ...
}