从iPhone Application Expressjs / Nodejs后端发送JSON发布数据
这是我的代码发送一个postjs后端请求。
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
来访问它,因为它返回为undefined。 我希望身体看起来像:
{ "lat":0.0, "lng":0.0}
任何想法如何使用快递做到这一点?
愿这帮助你。
请用您的实际坐标代替0.0
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); }
问题是,使用NSJSONSerialization
获取包含JSON数据的NSData对象后,您可以从该数据创buildpostString
。 消除这个不必要的步骤,只是做:
[request setHTTPBody:jsonData];
你应该在你的服务器端代码中得到预期的JSON。
- 使用Angular.js和iOS客户端对Node.js应用程序进行身份validation
- 在iOS上运行Node.js作为服务器,并让其他设备连接
- Swift – Spritekit:当节点触及底部边缘时,添加一个计数器(高分计算器)
- 如何使用Browserify和iOS JavaScriptCore
- iPhone / Android不会解雇用DNS实现的wifi登陆页面
- 使用VS2015编译共享库:“此客户端与配对的构build代理不兼容”
- Cordova after_prepare挂钩在Android中成功,但在iOS中失败
- 是可以开发在Android和iOS平台上运行的通用业务逻辑?
- 标题和POST正文之间的随机延迟