这是我的发送请求到nodejs后端的代码。从iPhone应用程序Expressjs/Nodejs后端发送JSON发布数据
CLLocation* location = [locationManager location];
CLLocationCoordinate2D coord = [location coordinate];
NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://50.63.172.74:8080/points"]];
//[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
NSDictionary* jsonDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:coord.latitude], @"lat", [NSNumber numberWithFloat:coord.longitude], @"lng", nil];//dictionaryWithObjectsAndKeys:coord.latitude, nil]
NSString *postString;
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
postString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
(void)[[NSURLConnection alloc] initWithRequest:request delegate:self];
使用快递,我发现了request.body回服务器上,但它看起来是这样的:
{ '{\n "lat" : 0.0,\n "lng" : 0.0\n}': '' }
而且由于它回来,因为我不能随便说request.body.lat
访问未定义。 我想身体看起来像:
{ "lat":0.0, "lng":0.0}
如何,使用快递做任何想法?
当让你的字典使用的setObject更换0.0:关键: http://stackoverflow.com/questions/9483872/iphone-json-message-not正确创建 对不起,我从我的手机提交 – badger0053