Home » Jquery » jquery – How to read excel file into json and send to CFC for database insertion?

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

Posted by: admin February 22, 2020 Leave a comment

Questions:

I am reading an excel file using “xlsx.full.min.js” plugin and getting the file data into JSON object.Then I am sending this JSON object to Coldfusion Component (CFC) using jQuery Ajax , but it gives me an error as ‘400 Bad request’. The CFC is not getting the JSON data that I am trying to send. What is wrong in my code?

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);
              });

And my CFC function is:

<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>
How to&Answer:

Update: I solved this error by using the deserializeJSON() method in my CFC to decode the json received from javascript.