ios – AFNetworking 2响应错误(内容类型:texthtml而不是JSON)

ios – AFNetworking 2响应错误(内容类型:texthtml而不是JSON),第1张

概述在尝试了几乎每一个关于这个问题的回答后,我都没有找到解决问题的方法. 问题:所以我在从AFNetworking 1.3移植后使用AFNetworking 2.0.3实现了我的应用程序的上传部分: -(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock 在尝试了几乎每一个关于这个问题的回答后,我都没有找到解决问题的方法.

问题:所以我在从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顶部使用’header(‘Content-type:application / Json’)'(PHP API).现在,如果我将序列化类型更改为’AFhttpResponseSerializer’而不是’AFJsONResponseSerializer’,它将不会吐出JsON错误,但它会给我一个不同的错误(随机无法识别的选择器错误).

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

解决方法 您可以将AFhttpSessionManager设置为接受任何MIME类型:

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/HTML"];
总结

以上是内存溢出为你收集整理的ios – AFNetworking 2响应错误(内容类型:text / html而不是JSON)全部内容,希望文章能够帮你解决ios – AFNetworking 2响应错误(内容类型:text / html而不是JSON)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1071873.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存