如何将 excel 文件读入 json 并发送到 CFC 以插入数据库?

How to read excel file into json and send to CFC for database insertion?

我正在使用 "xlsx.full.min.js" 插件读取 excel 文件并将文件数据导入 JSON object.Then 我正在将此 JSON 对象发送到 Coldfusion使用 jQuery Ajax 的组件 (CFC),但它给了我一个错误“400 错误请求”。 CFC 未收到我​​尝试发送的 JSON 数据。我的代码有什么问题?

var selectedFile = evt.target.files[0];
                    var reader = new FileReader();

                    reader.onload = function(event) {

                          var data = event.target.result;
                          var workbook = XLSX.read(data, {
                              type: 'binary'
                          });

                          workbook.SheetNames.forEach(function(sheetName) {
                                 var excel_json = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);

                                var json_object = JSON.stringify(excel_json);

                                $.ajax({
                                    url:"maps.cfc",
                                    dataType: "json",
                                    data: {
                                        method:"updateFuelPrices",
                                        jsstruct:json_object
                                    },

                                    success: function(data) {
                                        console.log(data);
                                        console.log("update successful");
                                    },
                                    error: function(xhr, textStatus, errorThrown) {
                                        console.log(textStatus);
                                        console.log(errorThrown);
                                        console.log("Something went wrong! Please refresh the page and try again.");
                                    }
                                })
                            })
                    };

                    reader.onerror = function(event) {
                      console.error("File could not be read! Code " + event.target.error.code);
                    };

                    reader.readAsBinaryString(selectedFile);
              });

而我的 CFC 函数是:

<cffunction name="updateFuelPrices"  output="false" access="remote" returnformat="json" returntype="string">
        <cfargument name="jsstruct" type='string' required="true">
        <cfset returntext = 'json sent successfully...'>
        <cfdump var="#ARGUMENTS.jsstruct#">
         <!--- DB insert query to go here --->
        <cfreturn returntext>
    </cffunction>

更新:我通过在我的 CFC 中使用 deserializeJSON() 方法来解码从 javascript.

收到的 json 来解决这个错误