AFNetworking 2响应错误(内容类型:text / html而不是JSON)

时间:2021-10-14 21:25:20

After trying nearly every response on the subject, I've come up without a working answer to my problem.

在尝试了几乎每一个关于这个主题的回答后,我都没有找到解决问题的方法。

The problem: So I've implemented the uploading portion of my app using AFNetworking 2.0.3 after porting from AFNetworking 1.3:

问题:所以我在从AFNetworking 1.3移植后使用AFNetworking 2.0.3实现了我的应用程序的上传部分:

-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock {

    NSData* uploadFile = nil;
if ([params objectForKey:@"file"]) {
    uploadFile = (NSData*)[params objectForKey:@"file"];
    [params removeObjectForKey:@"file"];
}

 AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://54.204.17.38"]];

 manager.responseSerializer = [AFJSONResponseSerializer serilizer];
 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];

 AFHTTPRequestOperation *apiRequest = [manager POST:@"/API" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

     if (uploadFile) {
         [formData appendPartWithFileData:uploadFile name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
     }

} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];

[apiRequest start];

}

The error I get when using this code is "Request failed: unacceptable content-type: text/html" I know you might be wondering if the server is responding with proper JSON, and I have every reason to think it is after inspecting the response headers in my browser that say 'MIME type: application/json'. Also, I am using 'header('Content-type: application/json')' at the top of my API as well (PHP API). Now, if I change the serialization type to 'AFHTTPResponseSerializer' instead of 'AFJSONResponseSerializer', it will not spit out the JSON error, but it will give me a different error (a random unrecognized selector error).

我在使用此代码时遇到的错误是“请求失败:不可接受的内容类型:text / html”我知道您可能想知道服务器是否正在使用正确的JSON进行响应,并且我完全有理由认为它是在检查响应之后我的浏览器中标题为'MIME type:application / json'的标题。此外,我在我的API顶部使用'header('Content-type:application / json')'(PHP API)。现在,如果我将序列化类型更改为'AFHTTPResponseSerializer'而不是'AFJSONResponseSerializer',它将不会吐出JSON错误,但它会给我一个不同的错误(随机无法识别的选择器错误)。

Any thoughts on why I cannot seem to get a JSON response out of this method?

对于为什么我似乎无法通过此方法获得JSON响应的任何想法?

2 个解决方案

#1


5  

You can set the AFHTTPSessionManager to accept any MIME Type:

您可以将AFHTTPSessionManager设置为接受任何MIME类型:

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

#2


1  

Got it! So, turns out, unknowingly, although my API was returning valid JSON, matter examining the header response logged on the Xcode side of things (thru NSLog(@"Error: %@", error);), it was actually returning text/HTML because it wasn't actually hitting the correct file, it was getting re-routed by a header somewhere. After explicitly stating the API path to be /API/index.php and not just /API, it started returning the valid JSON! Next, after making sure the response was properly JSON serialized (using requestManager.responseSerializer = [AFJSONResponseSerializer serializer];), the app worked!

得到它了!所以,在不知不觉中,尽管我的API返回了有效的JSON,但是检查记录在Xcode端的事件(通过NSLog(@“错误:%@”,错误);),它实际上是返回文本/ HTML,因为它实际上没有击中正确的文件,它被某个地方的标题重新路由。在明确说明API路径为/API/index.php而不仅仅是/ API之后,它开始返回有效的JSON!接下来,确保响应正确JSON序列化后(使用requestManager.responseSerializer = [AFJSONResponseSerializer序列化程序];),该应用程序工作!

Hopefully this helps someone who was having the same issue :)

希望这有助于有同样问题的人:)

#1


5  

You can set the AFHTTPSessionManager to accept any MIME Type:

您可以将AFHTTPSessionManager设置为接受任何MIME类型:

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

#2


1  

Got it! So, turns out, unknowingly, although my API was returning valid JSON, matter examining the header response logged on the Xcode side of things (thru NSLog(@"Error: %@", error);), it was actually returning text/HTML because it wasn't actually hitting the correct file, it was getting re-routed by a header somewhere. After explicitly stating the API path to be /API/index.php and not just /API, it started returning the valid JSON! Next, after making sure the response was properly JSON serialized (using requestManager.responseSerializer = [AFJSONResponseSerializer serializer];), the app worked!

得到它了!所以,在不知不觉中,尽管我的API返回了有效的JSON,但是检查记录在Xcode端的事件(通过NSLog(@“错误:%@”,错误);),它实际上是返回文本/ HTML,因为它实际上没有击中正确的文件,它被某个地方的标题重新路由。在明确说明API路径为/API/index.php而不仅仅是/ API之后,它开始返回有效的JSON!接下来,确保响应正确JSON序列化后(使用requestManager.responseSerializer = [AFJSONResponseSerializer序列化程序];),该应用程序工作!

Hopefully this helps someone who was having the same issue :)

希望这有助于有同样问题的人:)