0

我正在尝试从 Objective-C 和 JSONKit 发布到 Rails 后端,并且很难发布我的结果。我不断从我的服务器取回一个空记录集。

[dictionary setValue:(@"bar")  forKey:@"foo"];

NSString *JSON = [dictionary JSONString];
NSData *theData = [JSON dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString: myUrl];

NSString *postLength = [NSString stringWithFormat:@"%d", [theData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSError *error = NULL;
NSURLResponse *response = nil;

[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json-rpc" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:theData];
NSData *result = [NSURLConnection sendSynchronousRequest:request
                                       returningResponse:&response error:&error];

NSString *resultString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSLog(resultString);

有什么我想念的吗?JSON似乎正确序列化

{"foo":"bar"}

任何帮助将不胜感激。
谢谢!

4

1 回答 1

1

只需将 setValue 从 json-rpc 更改为 json ,它就像一个冠军。

[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
**[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];**
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[request setHTTPBody:theData];
于 2011-09-26T23:33:03.210 回答