无法在AFNetworking中的POST请求中发送参数

时间:2015-01-21 23:01:45

标签: objective-c 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);
}];
}

感谢您的任何建议

1 个答案:

答案 0 :(得分:0)

根据我使用AFNetworking的经验,这通常意味着您将获得非JSON响应。这可能类似于403或只是格式错误的JSON。你在看流量回来吗?