My current project involves the importing of an excel file in xlsx format. The file is converted into an array of objects, with each object representing a row from the excel sheet. I then utilize a forEach
loop to iterate over the array and create a data object based on each excel row. Here is an example:
resultArray.forEach((arr) => {
let data = {
action: 'addPestChem',
crop: arr['Crop'] ? arr['Crop'] : null,
company_name: arr['Company Name'] ? arr['Company Name'] : null,
product: arr['Product Name'] ? arr['Product Name'] : null,
substance: arr['Substance'] ? arr['Substance'] : null,
function: arr['Function'] ? arr['Function'] : null,
pcs_no: arr['PCS No'] ? arr['PCS No'] : null,
phi: arr['PHI'] ? arr['PHI'] : null,
use_by_date: arr['UseByDate'] ? arr['UseByDate'] : null,
date_of_reg_review: arr['DateofRegReview'] ? arr['DateofRegReview'] : null,
off_label_approval: arr['OffLabelApproval'] ? arr['OffLabelApproval'] : null,
latest_time_of_application: arr['LatestTimeOfApplication'] ? arr['LatestTimeOfApplication'] : null,
max_individual_dose: arr['MaxIndividualDose'] ? arr['MaxIndividualDose'] : null,
max_total_dose: arr['MaxTotalDose'] ? arr['MaxTotalDose'] : null,
max_no_applications: arr['MaxNoApplications'] ? arr['MaxNoApplications'] : null,
method_of_application: arr['Methodof Applic'] ? arr['Methodof Applic'] : null,
content: arr['Content'] ? arr['Content'] : null,
comment: arr['Comments'] ? ['Comments'] : null
};
console.log(data);
$.ajax({
url: "/assets/ajax/excel_ajax_handler.php",
type: "POST",
data
})
.done((res) => {
console.log(res);
})
.fail(function($xhr) {
var data = $xhr.responseJSON;
alert('An Error occured: ');
console.log(data.message);
});
});
After creating the data
object, an ajax call is made to a handler. Currently, for testing purposes, the handler echoes back '1'. However, upon running the code, '1' is logged around 118 times before the .fail()
function starts triggering. Any insights on why this behavior occurs?