這是我的發送請求到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