我正在尝试从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” 的: “酒吧”}
非常感谢任何帮助 谢谢!
答案 0 :(得分: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];