无法在 AFNetworking 的 POST 请求中发送参数
Can't send parameters in POST request in AFNetworking
我在玩 AFNetworking。在他们的示例中:http://cocoadocs.org/docsets/AFNetworking/2.5.0/ 有
NSDictionary *parameters = @{@"foo": @"bar"};
当我将这个变量打印到控制台时,它显示为
{
foo = bar;
}
一切正常。如果我将我的 NSMutableDictionary 传递给此方法,其中包含
{
apikey = 123;
stationId = 6;
}
我得到这个异常:Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7bf54c60 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
真不知道哪里出了问题。 (我是 Objective C 和 AFNetworking 的初学者,很抱歉我的无知:))
完整代码如下:
- (void) POST:(NSString *) url withData:(NSMutableDictionary *) jsonData
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *fullUrl = [self.baseUrl stringByAppendingString:url];
[jsonData setValue:self.apiKey forKey:@"apikey"];
NSLog(@"%@", [jsonData description]);
NSDictionary *parameters = @{@"stationId": @"6", @"apikey" : @"fdfd"};
NSLog(@"%@", [parameters description]);
[manager POST:fullUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
感谢任何建议
根据我使用 AFNetworking 的经验,这通常意味着您收到非 JSON 响应。这可能类似于 403 或格式错误的 JSON。你在看返回的流量吗?
我在玩 AFNetworking。在他们的示例中:http://cocoadocs.org/docsets/AFNetworking/2.5.0/ 有
NSDictionary *parameters = @{@"foo": @"bar"};
当我将这个变量打印到控制台时,它显示为
{
foo = bar;
}
一切正常。如果我将我的 NSMutableDictionary 传递给此方法,其中包含
{
apikey = 123;
stationId = 6;
}
我得到这个异常:Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7bf54c60 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
真不知道哪里出了问题。 (我是 Objective C 和 AFNetworking 的初学者,很抱歉我的无知:))
完整代码如下:
- (void) POST:(NSString *) url withData:(NSMutableDictionary *) jsonData
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *fullUrl = [self.baseUrl stringByAppendingString:url];
[jsonData setValue:self.apiKey forKey:@"apikey"];
NSLog(@"%@", [jsonData description]);
NSDictionary *parameters = @{@"stationId": @"6", @"apikey" : @"fdfd"};
NSLog(@"%@", [parameters description]);
[manager POST:fullUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
感谢任何建议
根据我使用 AFNetworking 的经验,这通常意味着您收到非 JSON 响应。这可能类似于 403 或格式错误的 JSON。你在看返回的流量吗?