我正在使用javascript Promise
,但仍然会在ajax
之前执行捕获仍然是我做错了什么?
这是我的代码
$(document).on("click", "#btn-group-agent-upload", function () {
var file = $("#group-agent-file").val();
var file = $("#group-agent-file").prop("files")[0];
var reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = Insert_Bulk_Agent;
});
这是函数
function Insert_Bulk_Agent(event) {
var result = event.target.result;
var data = $.csv.toObjects(result);
var length = data.length;
var count = 0;
var arrayCollection = [];
new Promise((resolve, reject) => {
if (length > 0) {
$.each(data, function (key, val) {
if(val.AgentStatus == "" || val.BirthDate == "" || val.CSP == "" || val.Classification == "" || val.ContactNumber == "" ||
val.EmailAddress == "" || val.FirstName == "" || val.LastName == "" || val.MiddleName == "" || val.LiveDate == "") {
$.ajax({
success: function () {
//console.log('ok');
//var tbody = $("#error-list").find("tbody");
//$(tbody).append("<tr><td>" + val.MiddleName + "</td><td>" + val.LastName + "</tr>");
arrayCollection.push(val);
console.log('test');
}
});
} else {
$.ajax({
xhr: function () {
var xhr = $.ajaxSettings.xhr();
xhr.upload.onprogress = function (evt) {
count = count + 1;
var percentageCompleted = count / length * 100;
updateUploadPercentage(parseInt(percentageCompleted));
};
return xhr;
},
type: "POST",
url: "IROA_StoredProcedures.asmx/Insert_Bulk_Agent",
data: JSON.stringify(val),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
},
error: function (XMLHttpRequest) {
alert("error in Insert_Bulk_AgentInfo()");
console.log(XMLHttpRequest);
}
});
}
});
}
else {
alert("The selected file is empty");
}
resolve();
}).catch(() => {
console.log(arrayCollection[0]);
})
}
arrayCollection[0]
返回undefined