Laravel - 错误:500。用Ajax更新记录

时间:2018-04-29 16:48:54

标签: javascript laravel-5

Laravel 5.5

我想在vanilla js中使用ajax更新一行。我有一个错误500 (internal server error)。是由于CSRF令牌吗?如果是这样,我如何添加它以发送更新?

以下是代码;

// 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);

0 个答案:

没有答案