2013-03-30 102 views
1

这是我的发送请求到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} 

如何,使用快递做任何想法?

+0

当让你的字典使用的setObject更换0.0:关键: http://stackoverflow.com/questions/9483872/iphone-json-message-not正确创建 对不起,我从我的手机提交 – badger0053

回答

4

愿这帮助你。

请与您的实际coordiantes

NSArray *keys = [NSArray arrayWithObjects:@"lat", @"lng", nil]; 
     NSArray *objects = [NSArray arrayWithObjects:@"0.0",@"0.0", nil]; 

     NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 
     NSData *jsonData ; 
     NSString *jsonString; 
     if([NSJSONSerialization isValidJSONObject:jsonDictionary]) 
     { 
      jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil]; 
      jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; 
     } 


     NSString *requestString = [NSString stringWithFormat: 
            @"http://50.63.172.74:8080/points"]; 

     NSURL *url = [NSURL URLWithString:requestString]; 
     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
     [request setHTTPMethod:@"POST"]; 
     [request setHTTPBody: jsonData]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
     [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; 

     NSError *errorReturned = nil; 
     NSURLResponse *theResponse =[[NSURLResponse alloc]init]; 
     NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned]; 

     if (errorReturned) { 
      NSLog(@"Error %@",errorReturned.description); 
     } 
     else 
     { 
      NSError *jsonParsingError = nil; 
      NSMutableArray *arrDoctorInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&jsonParsingError]; 

      NSLog(@"Dict %@",arrDoctorInfo); 
     } 
+0

同意...很好的解释。 – Siten

0

问题是,在使用NSJSONSerialization获取包含JSON数据的NSData对象后,您将从该数据创建postString。消除那不必要的步骤,只是:

[request setHTTPBody:jsonData]; 

而你应该在你的服务器端代码中得到预期的JSON。