2014-02-21 210 views
2

我正在研究一个iOS应用(对iOS很新颖),我有我的注册流程工作,我发布数据到我的服务器...服务器返回JSON,但我的问题是,我如何将它解析为变量?从发布请求ios afnetworking解析json

它返回的数据:

{"token":"67f41f60-9a9a-11e3-8310-11b6baf18c40","userId":13,"stationId":1} 

我使用作出POST请求的代码是:

[manager POST:@"http://localhost:3000/service/register_user/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"JSON: %@", responseObject); 


} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error!!!!!: %@", error); 
}]; 

而且,当我提出发布请求,这是什么我得到的日志在Xcode

2014-02-20 20:50:39.799 FlashoverResponse[4668:70b] Error!!!!!: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x8a5e4f0 {NSErrorFailingURLKey=http://localhost:3000/service/register_user/, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8a5e200> { URL: http://localhost:3000/service/register_user/ } { status code: 200, headers { 
Connection = "keep-alive"; 
"Content-Length" = 74; 
"Content-Type" = "text/html; charset=utf-8"; 
Date = "Fri, 21 Feb 2014 01:50:39 GMT"; 
"X-Powered-By" = Express; 

}},NSLocalizedDescription =请求失败:无法接受的内容类型:text/html的}

+0

您的服务器需要使用内容类型“application/json”进行响应。任何现代的MVC或Web API框架将使这非常简单。 –

回答

4

是否将您的服务器配置为在text/jsonapplication/json中进行响应?

和/或你有没有设置?

manager.responseSerializer.acceptableContentTypes

需要的内容类型此解析器将尝试解析,如果你不能改变服务器json回应,您可以添加text/html成集的NSSet,所以解析器无论如何都会解析它。

+0

我其实并没有......愚蠢的错误。谢谢 – Bill