AFNetWorking发送post请求,Code=-1016错误

  使用AFNetWorking发送post请求时,可能会出现下面Code=-1016问题。打印的error如下:

Error:Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x7fba3b6f1ba0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7fba3d8822c0> { URL: http://www.joinbuy.cn/api/login.ios.php?token=15x4874otutDRYxofue8&act=login } { status code: 200, headers {
Pragma = no-cache,
X-Powered-By = PHP/5.2.17,
Server = Apache/2.2.24 (Unix) PHP/5.2.17,
Set-Cookie = deal_city=deleted; expires=Sun, 07-Sep-2014 08:28:41 GMT; path=/api/,
Keep-Alive = timeout=5, max=100,
Connection = Keep-Alive,
Date = Mon, 07 Sep 2015 08:28:42 GMT,
Content-Type = text/html; charset=utf-8,
Content-Length = 46,
Expires = Thu, 19 Nov 1981 08:52:00 GMT,
Cache-Control = no-store, no-cache, must-revalidate, post-check=0, pre-check=0
} }, NSErrorFailingURLKey=http://www.joinbuy.cn/api/login.ios.php?token=15x4874otutDRYxofue8&act=login, com.alamofire.serialization.response.error.data=<7b226d73 67223a22 5c753864 32365c75 35336637 5c753562 63365c75 37383031 5c753935 31395c75 38626566 227d>, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}

  AFNetWorking的JSON解析默认库是使用的AFJSONRequestOperation模式,只支持text/json,application/json,text/javascript,所以如果出现code=-1016错误则说明当前的JSON解析模式是text/html,所以要加上这段代码:
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];

或者

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

让他支持text/html模式就可以解决了

原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/4789269.html