我为同一个问题找了几个答案......但是无法弄清楚这个问题......我收到了这个错误
*由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:'* + [NSJSONSerialization dataWithJSONObject:options:error:]:JSON中的顶级类型无效 写“
我的代码是:
NSDictionary *loginDict=[[NSDictionary alloc] initWithObjectsAndKeys:password.text,@"password",userName.text,@"userName",nil];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:loginDict options:NSJSONWritingPrettyPrinted error:&error];
if (jsonData) {
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
} else {
NSLog(@"Got an error: %@", error);
}
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setTimeoutInterval:20];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager POST:baseUrlLogin parameters:jsonString
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
答案 0 :(得分:3)
试试这个
直接在通话中传递loginDict
并检查
manager.requestSerializer = [AFJSONRequestSerializer serializer];
/*
[manager.requestSerializer setTimeoutInterval:20];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
*/
[manager POST:baseUrlLogin parameters: loginDict
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];