【发布时间】:2014-01-15 11:20:30
【问题描述】:
我正在使用 AFNetworking 2.0 从我正在 Node.js 中构建的服务中读取 JSON(目前在 localhost 上)。很正常的东西。
节点像这样发送 JSON:
res.setHeader('Content-Type','application/json');
res.end( JSON.stringify(...));
我的 iOS 首次通过代码正在尝试读取该数据,如下所示:
typedef void(^NextBlock)();
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:self.newestTimestampURL.absoluteString
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
//NSDictionary *response =
NSLog(@"got %@", responseObject );
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"fail %@", error );
}];
这是我得到的错误:
Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo=0xb783e30 {NSErrorFailingURLKey=http://localhost:3000/api/v1/log/newest, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xb656a70> { URL: http://localhost:3000/api/v1/log/newest } { status code: 200, headers {
Connection = "keep-alive";
ContentType = "application/json";
Date = "Fri, 27 Dec 2013 20:58:50 GMT";
"Transfer-Encoding" = Identity;
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/plain}
我可以 curl (-i) url http://localhost:3000/api/v1/log/newest 并获取我期望的数据,它是预期的 application/json。如果我在我的网络浏览器中加载该数据,我会按照开发工具检查器的预期获得 JSON。
但是使用 AFNetworking,我得到了这个神秘的“不可接受的内容类型:文本/纯文本”错误。有什么想法吗?
注意:我以前从未使用过 AFNetworking,所以我可能使用不正确。
【问题讨论】:
-
如果你
curl带有-I标志来获取标题。此时响应是否获得了正确的内容类型标头? -
当我 curl -I 我得到以下信息:
HTTP/1.1 200 OK ContentType: application/json Date: Sat, 28 Dec 2013 14:26:04 GMT Connection: keep-alive -
不应该是
Content-Type带连字符吗? -
哦,天哪。谢谢!我曾认为我的客户端代码有问题,但这是节点 JS 代码中的错字。非常感谢!圣诞奇迹!
标签: ios json node.js ios7 afnetworking-2