简体   繁体   English

Firefox 52:JavaScript parseFloat无法与UTF-16字符串一起使用

[英]Firefox 52: Javascript parseFloat not working with UTF-16 strings

I have been trying to read the contents of a UTF-16 file in Firefox v52 with this: 我一直在尝试使用以下方法在Firefox v52中读取UTF-16文件的内容:

$.ajax({
    type: "GET",
    url: "downloads/dg_mare_piwik_social.csv",
    dataType: "text",
    success: function(data) {

        // Split the lines

        var lines = data.split('\n');
        for (var i = 1, len = lines.length; i < len; ++i) {
            var items = lines[i].split(',');
            console.log(items[0], Number.parseFloat(items[1]));
        }
});

The problem is that in the console I get NaN from the parseFloat(items[1]) . 问题是在控制台中,我从parseFloat(items[1])获得了NaN This happens only in Firefox, all other browsers get the number with that. 仅在Firefox中会发生这种情况,所有其他浏览器都会获得该数字。

How can I get it to work? 我如何使它工作? How can I get the number from a UTF-16 string? 如何从UTF-16字符串中获取数字?

Kudos to TJ Crowder that helped me to identify the source of the problem. TJ Crowder的荣誉帮助我确定了问题的根源。 Changing it to this has solved my problem: 将其更改为此解决了我的问题:

$.ajax({
    type: "GET",
    url: "downloads/dg_mare_piwik_social.csv",
    dataType: "text",
    beforeSend: function (data) { data.overrideMimeType('text/plain; charset=UTF-16'); },
    success: function(data) {

        // Split the lines

        var lines = data.split('\n');
        for (var i = 1, len = lines.length; i < len; ++i) {
            var items = lines[i].split(',');
            console.log(items[0], Number.parseFloat(items[1]));
        }
});

Adding the overrideMimeType at beforesend. 在beforesend处添加overrideMimeType。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM