AFNetworking 2.0 POST issue, Cocoa error 3840(JSON text did not start with array…)

泪湿孤枕 提交于 2019-12-01 17:14:11

When you use the API as in your example, your HTTP message will be composed by using a Content-Type application/x-www-form-urlencoded. Internally, your param dictionary are encoded by AFN (although not strictly correct as specified by w3c), and set as the request body.

Since, you did not specify an Accept header, the server is free to choose the content type for a possible response data (if any).

When you receive a response, you need to always check the HTTP status code and the content type of a response body (if any).

It's likely, the server returned a status code indicating some issue and a response body containing an "error response" in some different kind of content type than you expect (for example, it returned text/html).

You may use smth like wireshark (http://www.wireshark.org/) while running app in simulator or on device connected via shared wifi to mac you're running whireshark on to trace actual requests and responses.

Waqar Ahmed

This works for me:

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

 [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    manager.requestSerializer = requestSerializer;

[manager POST:URLString
       parameters:params
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              NSLog(@"JSON: %@", responseObject);
              [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"Error: %@", error);
              [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
          }];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!