Have you done the spoof for the put method.
Apr 23, 2021
20
Level 1
Laravel 8 Ajax Update Record
Hey Guys i was trying to update order status with ajax & Bootstrap Modal. But I got that error after submitting to update.
I got this code from internet because i'm not good at ajax.. and not professional in Laravel tbh
Uncaught TypeError: Illegal invocation
function editStatus(e) {
var id = $(e) .data("id");
var status = $("#order_"+id+" td:nth-child(2)") .html();
$("#id") .val(id);
$("#status") .val(status);
$('#orderEditModal') .modal('show');
}
function updateStatus() {
var status = $('#status') .val();
var id = $('#id') .val();
let _url = `/admin/orders/${id}`;
let _token = $('meta[name="csrf-token"]').attr('content');
$.ajax({
url: _url,
type: "PUT",
data: {
status: status,
user_id: user_id,
_token: _token
},
success: function(data) {
status = data
$("#order_"+id+" td:nth-child(2)") .html(status .status);
$('#id').val('');
$('#EditModal').modal('hide');
}
});
}
There is a strange spaces that's because i cant put any link
Level 75
Here is a working jquery example, (just example):
$(function () {
$("#postjq").click(function (event)
{
event.preventDefault();
var $post = {};
$post.petid = $('#petid').val();
$post.species = $('#species').val();
$post.ocheck = ($("#ocheck").prop("checked") == true ? '1' : '0');
$post._token = document.getElementsByName("_token")[0].value
$post._method = document.getElementsByName("_method")[0].value
$.ajax({
url: '<?= DIR . "pet/petupdate" ?>',
type: 'PUT',
data: $post,
cache: false,
success: function (data) {
var message = "data updated";
alertWithoutNotice(message);
$('#msg').append('<div>' + data.success + '</div>');
},
error: function (data, ajaxOptions, thrownError) {
var status = data.status;
if (data.status === 422) {
$.each(data.responseJSON.errors, function (key, value) {
$('#msg').append('<div>' + value + '</div>');
});
}
if (status === 403 || status === 500) {
$('#msg').text("Not Auth");
}
}
});
});
function alertWithoutNotice(message) {
setTimeout(function () {
alert(message);
}, 1000);
}
});
Note
I don't use json in this example, I use an array. But it works, you can adapt as needed and use json if you want.
I am moving to fetch js, that's why I gave you the other example.
1 like
Please or to participate in this conversation.