dimanche 29 avril 2018

Laravel - Error: 500. Update record with Ajax

In Laravel 5.5,

I want to update a row using ajax in vanilla js. I have an error, 500 (internal server error). Is it due to the CSRF token? If so, how do I add it to send for the update?

Below are the codes;

// I have an object that stores:
params = {
    method: "PUT", 
    _token: "uq1Ipn0ehw9Ias5vsgy1pT3ckq4OhdmNMqmpGu20", 
    account: "my account", 
    username: "user1"
}

let data = '';
//params._method = "PUT";
//params._token = document.getElementsByTagName('input').item(name="_token").value;

for(let property in params){
    data += property + '=' + params[property] + '&'; 
}

data = data.slice(0, -1);

console.log(data);
// _method=PUT&_token=uq1Ipn0ehw9Ias5vsgy1pT3ckq4OhdmNMqmpGu20
// &account=my account&username=user1

var xhr = new XMLHttpRequest();
xhr.open('POST', '/personals/' + this.value, true);
// this.value will give the id of the row that will be update.eg. 1  
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = function(){
  console.log(this.responseText);
}

xhr.send(data);



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2r8f1Es
via IFTTT

Aucun commentaire:

Enregistrer un commentaire