Skip to content

Instantly share code, notes, and snippets.

@masiur
Last active October 17, 2017 12:00
Show Gist options
  • Select an option

  • Save masiur/7debc6689c07ca8bbc8f6cc26c4670c1 to your computer and use it in GitHub Desktop.

Select an option

Save masiur/7debc6689c07ca8bbc8f6cc26c4670c1 to your computer and use it in GitHub Desktop.
A perfect Ajax Call with function
<script>
function showToaster(type) {
setTimeout(function() {
toastr.options = {
closeButton: true,
progressBar: false,
positionClass: "toast-bottom-right",
showMethod: 'slideDown',
timeOut: 3000
};
if (type == "success"){
toastr.success('', "Role Updated");
} else {
toastr.error('', "Something went wrong. Please, try again");
}
}, 60);
}
$(document).ready(function() {
var userId = "{!! $user->id !!}";
var requestUri = "{!! route('user.roleUpdate') !!}";
$('#roleChange').on("click", '.clickableSwitch', function (e) {
var roleId = $(this).attr('roleId');
roleUpdate(userId, roleId);
});
function roleUpdate(userId, roleId){
/* console.log(userId, roleId); */
$.ajax({
// Switch off caching
cache: false,
//Set the type of request
type: "GET",
// Set the timeout
timeout: 5000,
// set url to which the request is sent
url: requestUri,
// define data type
dataType: "json",
// your data
data: {userId: userId, roleId: roleId},
// if the request is success get the response
success: function (response) {
showToaster("success");
},
// if the request is error get the response
error: function(response) {
showToaster("error");
}
});
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment