AFNetworking 2响应错误(内容类型:text/html而非JSON) [英] AFNetworking 2 Response Error (Content type: text/html and not JSON)
问题描述
在尝试了有关该主题的几乎所有答复后,我上来的问题仍未得到有效的解决.
问题:因此,从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];
}
使用此代码时出现的错误是请求失败:不可接受的内容类型:text/html",我知道您可能想知道服务器是否以正确的JSON
响应,我有充分的理由去思考是在检查浏览器中响应标题为"MIME type: application/json
"的标题之后.另外,我也在我的API(PHP API)顶部使用了'header('Content-type:application/json')'.现在,如果我将序列化类型更改为'AFHTTPResponseSerializer
'而不是'AFJSONResponseSerializer
',它不会吐出JSON error
,但是会给我一个不同的错误(随机的无法识别的选择器错误).>
关于为什么我似乎无法从这种方法中得到JSON
响应的任何想法?
您可以将AFHTTPSessionManager
设置为接受任何MIME类型:
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
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
:
-(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).
Any thoughts on why I cannot seem to get a JSON
response out of this method?
You can set the AFHTTPSessionManager
to accept any MIME Type:
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
这篇关于AFNetworking 2响应错误(内容类型:text/html而非JSON)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!