我想在get请求的标头中添加一个授权令牌(在此请求中我想从我的服务器获取一些JSON信息)这里是我的代码:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
//[manager.requestSerializer setAuthorizationHeaderFieldWithToken:token];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"4959a0bc00a15e335fb6" forHTTPHeaderField:@"Authorization"];
[manager GET:@"http://domain.com/recipes.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
NSError *e=nil;
recipesarray = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&e];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
我收到了这个错误: 该操作无法完成。 (可可误差3840。)" (JSON文本不以数组或对象和选项开头,以允许未设置片段。)UserInfo = 0x7f8098529760 {NSDebugDescription = JSON文本未以数组或对象开头,并且选项允许未设置片段。,NSUnderlyingError = 0x7f8098513790"请求失败:未经授权(401)"}
有谁知道原因? 非常感谢
答案 0 :(得分:0)
您不需要AFJSONRequestSerializer
您需要AFJSONResponseSerializer
request =>响应
和AFJSONResponseSerializer
是默认的responseSerializer,所以不要更改requestSerializer
答案 1 :(得分:-1)
使用AFHTTPRequestOperationManager使用方法setAuthorizationHeaderFieldWithToken
[manager.requestSerializer setAuthorizationHeaderFieldWithToken:token];