I am new to Javascript and jQuery. I am running into an issue passing data to my function, ProcessFileFromGrid() in the onClick event in JS. It all tests out fine, but then I wanted to do something with the result sent back from the Ajax call.
In this example I call API /uploadPhysical and then I check for sucess: or error:. That all works great.
In the Success: callback, I display a question to the user, “Upload Complete! Do you want to process your files?”. If they say yes, I do a series of things in the onClick event. One of the things that I do is pass “data” to the ProcessFilesFromGrid() function. It works fine without the data being passed, so I know its how I am passing “data”. data comes back from the call. I have tried different things, but I get a JS errors in F12 console. I also tried: ProcessFileFromGrid(‘” + data + “‘
How would I do this?
$.ajax({
type: "POST",
mimetype: 'multipart/form-data',
url: commonUrl + "api/FileTransfer/UploadPhysical",
headers: {
'Authorization': "Bearer " + token,
},
//dataType: 'json',
data: formData,
processData: false,
contentType: false,
async: true,
// cache: false,
// timeout: 600000,
success: function (data) {
var screenExitMessage = "Upload Completed!<br/><br/>Do you want to process your files ?";
alert(data);
swal.fire({
html:
"<button type='button' role='button' tabindex='0' class='btn swal2-styled btn-outline btn-outline-success' onclick='swal.close(); ProcessFilesFromGrid(data);'><i class='fa-solid fa-check fs-1'></i>Yes</button>" +
"<button type='button' role='button' tabindex='0' class='btn swal2-styled btn-light-primary' onclick='swal.close(); ReloadFileGrids();'>No</button>",
title: screenExitMessage,
text: screenExitMessage,
icon: 'question',
showCancelButton: false,
showConfirmButton: false,
}).then(function () {
// swal.close();
// ReloadFileGrids();
});
},
error: function (e) {
swal.fire({
text: e.responseText,
icon: "error",
buttonsStyling: false,
confirmButtonText: "OK",
customClass: {
confirmButton: "btn font-weight-bold btn-light-primary"
}
})
}
});
}
else {
swal.fire({
text: "Please select a file to upload",
icon: "error",
buttonsStyling: false,
confirmButtonText: "OK",
customClass: {
confirmButton: "btn font-weight-bold btn-light-primary"
}
})
}
function ProcessFilesFromGrid(databaseName) {
enableControls(false, false);
var username = localStorage.getItem("Username");
var orgId = $("#ddlBorrowerName").val();
var processOption = $("#ddlProcessOption").val();
var databaseOption = $("#ddlDatabaseOption").val();
if (databaseName) {
databaseOption = databaseName;
}
console.log("Process Option: " + processOption);
console.log("Database Option: " + databaseOption);
var apigetlink = "/Upload/ProcessFiles?Username=" + username + "&OrgId=" + orgId + "&ProcessOption=" + processOption + "&DatabaseOption=" + databaseOption;
AjaxPostBackendAsyncCallback(apigetlink, model, function (data) {
if (data && data.status == 1) {
swal.fire({
html: data.message,
icon: "success",
icon: "success",
buttonsStyling: false,
confirmButtonText: "OK",
customClass: {
confirmButton: "btn font-weight-bold btn-light-primary"
}
});
ReloadFileGrids();
}
else {
var message = data.message;
message = "Processing Issues!<br /><br />";
message += data.message.replaceAll("<br />","<br /><br />");
swal.fire({
html: message,
icon: "info",
buttonsStyling: false,
confirmButtonText: "OK",
customClass: {
confirmButton: "btn font-weight-bold btn-light-primary"
}
});
ReloadFileGrids();
}
}, errorCallback);
}