Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从iPhone应用程序Expressjs/Nodejs后端发送JSON post数据_Ios_Objective C_Node.js - Fatal编程技术网

Ios 从iPhone应用程序Expressjs/Nodejs后端发送JSON post数据

Ios 从iPhone应用程序Expressjs/Nodejs后端发送JSON post数据,ios,objective-c,node.js,Ios,Objective C,Node.js,这是我向nodejs后端发送post请求的代码 CLLocation* location = [locationManager location]; CLLocationCoordinate2D coord = [location coordinate]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://50.63.172.74:8080/points

这是我向nodejs后端发送post请求的代码

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];
使用express,我正在将request.body返回到服务器上,但看起来是这样的:

{ '{\n  "lat" : 0.0,\n  "lng" : 0.0\n}': '' } 
我不能通过说
request.body.lat
来访问它,因为它返回时是未定义的。 我想让身体看起来像:

{ "lat":0.0, "lng":0.0}

有没有关于如何使用express实现这一点的想法?

问题是,在使用
NSJSONSerialization
获取包含JSON数据的NSData对象后,您可以从该数据创建
postString
。消除不必要的步骤,只需执行以下操作:

[request setHTTPBody:jsonData];

您应该在服务器端代码中获得预期的JSON。

问题是,在使用
NSJSONSerialization
获取包含JSON数据的NSData对象后,您可以从该数据创建
postString
。消除不必要的步骤,只需执行以下操作:

[request setHTTPBody:jsonData];
您应该在服务器端代码中获得预期的JSON。

这可能会对您有所帮助

请用实际坐标替换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);
        }
愿这对你有帮助

请用实际坐标替换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);
        }

当您的字典使用setobject:for key时:对不起,我从我的电话提交的内容很简短当您的字典使用setobject:for key时:对不起,我从我的电话提交的内容很简短同意。。。很好的解释。同意。。。很好的解释。