Javascript 使用Jquery读取CSV文件

Javascript 使用Jquery读取CSV文件,javascript,jquery,file,csv,Javascript,Jquery,File,Csv,我需要从本地服务器读取CSV文件的内容。在我的CSV文件中,我有三个字段,它们是: 地区, 账户名称 国家 在获取Acct_中的值时,名称值的格式为逗号。在我的CSV文件读取器中,它是分隔逗号,该值将进入状态 那么我该如何解决这个问题呢 我的代码是: $(document).ready(function() { // AJAX in the data file $.ajax({ type: "GET", url: "data.csv",

我需要从本地服务器读取CSV文件的内容。在我的CSV文件中,我有三个字段,它们是:

  • 地区,
  • 账户名称
  • 国家
在获取Acct_中的值时,名称值的格式为逗号。在我的CSV文件读取器中,它是分隔逗号,该值将进入状态

那么我该如何解决这个问题呢

我的代码是:

$(document).ready(function() {
 // AJAX in the data file
    $.ajax({
        type: "GET",
        url: "data.csv",
        dataType: "text",
        success: function(data) {processData(data);}
        });

    // Let's process the data from the data file
    function processData(data) {
        var table = $("<table />");
                    var rows = data.split(/\r\n|\n/);
                    for (var i = 1; i < rows.length-1; i++) {
                        var row = $("<tr />");
                        var cells = rows[i].split(",");

                        for (var j = 0; j < rows.length; j++) {
                            var cell = $("<td />");
                            cell.html(cells[j]);
                            row.append(cell);
                        }

                          var usedNames = {};
                            $("select[name='company1'] > option").each(function () {
                                if(usedNames[this.text]) {

                                    $(this).remove();
                                } else {
                                    usedNames[this.text] = this.value;
                                }
                            });
                              $("select[name='company2'] > option").each(function () {
                                if(usedNames[this.text]) {

                                    $(this).remove();
                                } else {
                                    usedNames[this.text] = this.value;
                                }
                            });
                                $("select[name='company3'] > option").each(function () {
                                if(usedNames[this.text]) {

                                    $(this).remove();
                                } else {
                                    usedNames[this.text] = this.value;
                                }
                            });
                            $("#region").append("<option value =1> " + cells[0] + " </option>");
                            $("#state").append("<option value =1> " + cells[1] + "</option>");
                            $("#accname").append("<option value =1>" + cells[2] + "</option>");

                        table.append(row);
                    }

    }
});
$(文档).ready(函数(){
//数据文件中的AJAX
$.ajax({
键入:“获取”,
url:“data.csv”,
数据类型:“文本”,
成功:函数(数据){processData(数据);}
});
//让我们来处理数据文件中的数据
函数processData(数据){
变量表=$(“”);
var rows=data.split(/\r\n |\n/);
对于(变量i=1;i选项”)。每个(函数(){
如果(使用名称[this.text]){
$(this.remove();
}否则{
usedNames[this.text]=this.value;
}
});
$(“选择[name='company2']>选项”)。每个(函数(){
如果(使用名称[this.text]){
$(this.remove();
}否则{
usedNames[this.text]=this.value;
}
});
$(“选择[name='company3']>选项”)。每个(函数(){
如果(使用名称[this.text]){
$(this.remove();
}否则{
usedNames[this.text]=this.value;
}
});
$(“#区域”).append(“+单元格[0]+”);
$(“#state”).append(“+单元格[1]+”);
$(“#accname”).append(“+单元格[2]+”);
表.追加(行);
}
}
});

您遇到了什么错误?另外,您应该将数据类型更改为
text/csv
我已经更改了数据类型,但不工作,并且您在控制台中没有遇到任何错误?@panduk检查它