将变音符号从Node.js发送到iOS [英] Send umlauts from Node.js to iOS

查看:127
本文介绍了将变音符号从Node.js发送到iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我不知道这是Node.js还是iOS问题,但当我尝试发送包含变音符号(ä,ö和ü)的JSON对象时内容长度似乎是错误的。


I don't know if this is a Node.js or an iOS problem, but when I try to send a JSON object which contains umlauts (ä, ö, and ü) the set content-length seems to be wrong.

所以这是我的设置:

我的Node.js服务器发送数据通过:

My Node.js server sends data via:

[..mongodb request..].toArray(function(err, result) {
    res.setHeader('Content-Type', 'application/json');
    var body = JSON.stringify(result);

    console.log(body);
    console.log(body.length);

    res.setHeader('charset', 'utf8');
    res.setHeader('Content-Length', body.length);
    res.end(body);
});

这会产生以下对象:

[
{
    "_id": "51da7eb5d5f9ad77302a26c6",
    "loc": [
        53.560994,
        9.929796
    ],
    "street": "Kühnehöfe 25",
    "time": 1373273781535
},
{
    "_id": "51da7eb9d5f9ad77302a26c7",
    "loc": [
        53.561899,
        9.930203
    ],
    "street": "Kühnehöfe 17",
    "time": 1373273785156
}
]

其中(已解析为字符串)长度为215.这也是设置为内容长度。

Which has (parsed as string) a length of 215. This is also set as the content length.

在我的iOS项目中,我有以下设置:

In my iOS project I've got following setup:

-(void)serverRequest {

    // Initialize new mutable data
    NSMutableData *data = [[NSMutableData alloc] init];
    self.receivedData = data;

    // Initialize URL that is going to be fetched.
    NSURL *url = [NSURL URLWithString:@"http://localhost:8000/getSpots"];

    // Initialize a request from a URL
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];

    // Set HTTP method
    [request setHTTPMethod:@"POST"];

    // Initialize post data
    NSDictionary* jsonDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:coordinate.latitude], @"lat", [NSNumber numberWithFloat:coordinate.longitude], @"lng", accessToken, @"accessToken", nil];//dictionaryWithObjectsAndKeys:coord.latitude, nil]
    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];

    // Set request content type we MUST set this value.
    [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"content-type"];

    // Set post data of request
    [request setHTTPBody:jsonData];

    // Initialize a connection from request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.connectionGetSpots = connection;

    // Start the connection
    [connection start];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.receivedData appendData:data];

    NSLog(@"loading: %d, %lld", [self.receivedData length], dataSize);  // Both 215 (correct)
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    dataSize = [response expectedContentLength];
    NSLog(@"dataSize: %lld", dataSize);  // is 215 (correct)
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSString *responseData = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"rD: %@, %d" ,responseData, [responseData length]);
}

connectionDidFinishLoading 函数记录下:

[
{
    "_id": "51da7eb5d5f9ad77302a26c6",
    "loc": [
        53.560994,
        9.929796
    ],
    "street": "Kühnehöfe 25",
    "time": 1373273781535
},
{
    "_id": "51da7eb9d5f9ad77302a26c7",
    "loc": [
        53.561899,
        9.930203
    ],
    "street": "Kühnehöfe 17",
    "time": 13732737851
,211

As你可以看到,JSON对象中有四个变音符号,缺少四个字符。如果我添加另一个带有两个变音符号的位置,则会丢失另外两个字符。

As you can see, there are four umlauts in the JSON object and four characters are missing. If I add another location with two umlauts, two more characters will be missing.

我猜某个地方的内容类型设置错误,但我不知道我有什么要做的事。

I guess somewhere the content type is set wrong, but I'm not sure what I have to do.

推荐答案

使用它来获得正确的UTF-8长度:

Use this for correct UTF-8 lengths:

new Buffer(body).length

这篇关于将变音符号从Node.js发送到iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆