AFNetworking 2.0:无法使用JSON从IOS进行POST

自从我的IOS代码升级到使用AFNetworking版本2.0而不是1.x后,我无法再使用JSON参数进行HTTP发布。

我怀疑这是因为我的HTTP请求头不是“应用程序/ JSON”,但我试图做到这一点,但仍然无法正常工作。

这是我的testing代码尝试由JSON POST:

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys: email, @"email", password, @"password", nil]; [[OTApiClient sharedInstance] POST:@"login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"result: %@", responseObject); Boolean response = [[responseObject valueForKey:@"success"] boolValue]; if(response == TRUE) { //ok, success NSLog(@"success"); UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Successfully logged in" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } else { NSLog(@"Not successful"); UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"The email or password is incorrect." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } dispatch_async(dispatch_get_main_queue(), ^{ [MBProgressHUD hideHUDForView:self.view animated:YES]; }); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error: %@ , for operation: %@", error, operation); UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"There is a problem connecting to the server, please try again soon." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; dispatch_async(dispatch_get_main_queue(), ^{ [MBProgressHUD hideHUDForView:self.view animated:YES]; }); }]; }); 

我不断收到这个错误:

 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0xca80730 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} , for operation: <AFHTTPRequestOperation: 0xca6bee0, state: isFinished, cancelled: NO request: <NSMutableURLRequest: 0xca76040> { URL: http://1.2.3.4:9000/api/login }, response: <NSHTTPURLResponse: 0xc963d60> { URL: http://1.2.3.4:9000/error404 } { status code: 404, headers { "Content-Length" = 1809; "Content-Type" = "text/html; charset=utf-8"; 

}}>

我非常确定服务器已经启动并可以访问,因为所有其他GET调用都能正常工作。

我已经检查了AFNetworking 2.0的更新文档,看来我拥有正确的方式来做一个JSON POST

请让我知道,如果我错过了什么

谢谢你

其实我终于搞定了

基本上,对于AFHTTPRequestOperationManager的requestSerializer,我使用的是AFHTTPRequestSerializer,然后尝试将Content-Type设置为“application / json”

但是,一旦我切换到使用AFJSONRequestSerializer,而现在它工作正常

 AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; operationManagerInstance.requestSerializer = requestSerializer; 
 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer]; [serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; manager.requestSerializer = serializer; 

现在它会工作。

我遇到了同样的问题,通过Swift使用AFNetworking API,试图发送一个post请求,并最终得到它的工作,以及感谢user1805458的职位。

基本上,对于AFHTTPRequestOperationManager我的requestSerializer,我也使用AFHTTPRequestSerializer,然后尝试将Content-Type设置为“应用程序/ JSON”,它不起作用。

但是,一旦我切换到使用AFJSONRequestSerializer,而现在它工作正常:

  let manager = AFHTTPRequestOperationManager() manager.requestSerializer = AFJSONRequestSerializer(writingOptions: nil) // Contrary to user1805458's solution, I did not need to use these two instructions below using Swift. // manager.requestSerializer.setValue("Application/JSON", forHTTPHeaderField:"Content-Type") // manager.requestSerializer.setValue("Application/JSON", forHTTPHeaderField:"Accept") 

我希望它会帮助你解决这个错误的情况下,你使用Swift,AFNetworking,并尝试发送包含JSON正文的发布请求。