为 afnetworking 中的授权令牌设置请求 header
setting a request header for an authorization token in afnetworking
我想向获取请求的 header 添加授权令牌(在此请求中,我想从我的服务器获取一些 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);
}];
我收到这个错误:
操作无法完成。 (Cocoa 错误 3840。)”(JSON 文本未以数组或 object 开头,并且未设置允许片段的选项。)UserInfo=0x7f8098529760 {NSDebugDescription=JSON 文本未以数组或 object 开头,并且未设置允许片段的选项。NSUnderlyingError=0x7f8098513790 "Request failed: unauthorized (401)"}
有谁知道原因吗?
非常感谢
你不需要 AFJSONRequestSerializer
你需要 AFJSONResponseSerializer
请求=>响应
和 AFJSONResponseSerializer
是默认的 responseSerializer 所以不要更改 requestSerializer
使用 AFHTTPRequestOperationManager 使用方法 setAuthorizationHeaderFieldWithToken
[manager.requestSerializer setAuthorizationHeaderFieldWithToken:token];
我想向获取请求的 header 添加授权令牌(在此请求中,我想从我的服务器获取一些 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);
}];
我收到这个错误: 操作无法完成。 (Cocoa 错误 3840。)”(JSON 文本未以数组或 object 开头,并且未设置允许片段的选项。)UserInfo=0x7f8098529760 {NSDebugDescription=JSON 文本未以数组或 object 开头,并且未设置允许片段的选项。NSUnderlyingError=0x7f8098513790 "Request failed: unauthorized (401)"}
有谁知道原因吗? 非常感谢
你不需要 AFJSONRequestSerializer
你需要 AFJSONResponseSerializer
请求=>响应
和 AFJSONResponseSerializer
是默认的 responseSerializer 所以不要更改 requestSerializer
使用 AFHTTPRequestOperationManager 使用方法 setAuthorizationHeaderFieldWithToken
[manager.requestSerializer setAuthorizationHeaderFieldWithToken:token];