3

我正在使用 AFNetworking 向 Web 服务发出 POST 请求

这是我需要发送的 JSON-

{
   “LinkedTo” : {
                            “IndividualStakeholder” : “”,
                            “GroupedStakeholder” : “”,
                            “LandParcelStakeholder” : “”,
                            “Communications” : “”,
                            “Team” : “”,
                            “Issue” : “”,
                            “Event” : “”
                        }
   “userId” : 170,
   “projectId”: 12
}

这是我正在使用的代码 -

NSString *jsonData = [self JSONString:jsonData1];

此时的值NSString *jsonData

{\"LinkedTo\":{\"IndividualStakeholder\":\"\",\"GroupedStakeholder\":\"\",\"LandParcelStakeholder\":\"\",\"通讯\":\"\ ",\"团队\":\"\",\"问题\":\"\",\"事件\":\"\"},\"userId\":170,\"projectId\": 4}

NSURL *url = [[NSURL alloc]initWithString:@"https://somedomain.com/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                             jsonData, @"jsonText" , nil];
NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"https://somedomain.com/api/stakeholders" parameters:params];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
        NSLog(@"Inside the success block %@",JSON);
    }
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
        NSLog(@"json text is: %@", JSON);
        NSLog(@"Request failed with error: %@, %@", error, error.userInfo);
    }];
[operation start];



-(NSString*)JSONString :(NSString*)aString{
    NSMutableString *s = [NSMutableString stringWithString:aString];
    [s replaceOccurrencesOfString:@"\"" withString:@"\\\"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"/" withString:@"\\/" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\n" withString:@"\\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\b" withString:@"\\b" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\f" withString:@"\\f" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\r" withString:@"\\r" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\t" withString:@"\\t" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    return [NSString stringWithString:s];
}

服务器上的错误是- URL 意外以“/stakeholders”结尾的请求格式无法识别。反序列化发布的 JSON 时,asp.net 中的错误是什么

我在客户端收到的输出是

请求失败并出现错误:Error Domain=com.alamofire.networking.error Code=-1011 “预期状态代码 [索引数:100(在 1 个范围内),索引:(200-299)],得到 500”UserInfo=0x8e95e60 {NSErrorFailingURLKey= https://somedomain.com/api/stakeholders , NSLocalizedDescription=预期状态码 [索引数: 100 (在 1 个范围内), 索引: (200-299)], got 500}, { NSErrorFailingURLKey = " https ://somedomain.com/api/stakeholders "; NSLocalizedDescription = "预期状态码 [索引数:100(1 个范围内),索引:(200-299)],得到 500"; } 2013-02-25 11:03:44.669 网络插件[60069:c07]

4

2 回答 2

3

Check outAFHTTPClientsetParameterEncoding方法,AFJSONParameterEncoding所以帖子实际上将有一个有效的 JSON HTTP 正文。在这里声明和评论

于 2013-02-25T23:21:28.573 回答
1

我看到的是您发送","的不是诸如"15", "18"etc 之类的值。尝试使用正确的 JSONKit 函数修改您的 json 数据。

于 2013-02-25T19:49:44.487 回答