laravel ajax call code example
Example 1: ajax laravel example
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(".btn-submit").click(function(e){
e.preventDefault();
var name = $("input[name=name]").val();
var password = $("input[name=password]").val();
var email = $("input[name=email]").val();
$.ajax({
type:'POST',
url:'/ajaxRequest',
data:{name:name, password:password, email:email},
success:function(data){
alert(data.success);
}
});
});
Example 2: laravel ajax file
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
let formData = new FormData(form);
$.ajax({
type:'POST',
url:'{{ route('url') }}',
data: formData,
contentType: false,
processData: false,
cache: false,
beforeSend: function() {
$("#full_loading").show();
},
success: (response) => {
if (response) {
if(response.param == true) {
window.location.href = "{{ route('url','id') }}".replace("id", response.id);
}
}
},
error: function(response){
console.log(response);
}
});