After the request is complete, I want to run specific code.
I'm unsure if this is the correct approach. Can you provide guidance on this?
Thank you!
Handling Ajax Requests
function sendRequest(url, data, type) {
return $.ajax({
url: url,
dataType: 'json',
contentType: 'application/json; charset=UTF-8',
data: JSON.stringify(data),
type: type
}).done(function (data) {
console.log('success');
return true;
}).fail(function (jqXHR, textStatus, errorThrown) {
console.log('fail');
return false;
});
}
Making an Ajax Call
function newAuthor() {
var data = {
"last_name": $("#authorLastName").val(),
"first_name": $("#authorFirstName").val()
};
var result = sendRequest('/authors/', data, "POST");
if(result) {
// do something if true
}
else {
// do something if false
}
}