First working prototype of POST request from JavaScript to Laravel Controller
This commit is contained in:
parent
420cd3ce63
commit
07ce7961c9
2 changed files with 23 additions and 58 deletions
|
|
@ -88,62 +88,31 @@
|
|||
}
|
||||
alert("Tauschen:\n"+selected_players.join("\n"));
|
||||
|
||||
xmlhttp=new XMLHttpRequest()
|
||||
let url = '/league/swap';
|
||||
let token = '{{ csrf_token() }}'
|
||||
|
||||
var formData=new FormData()
|
||||
//alert(inputElems[0].id);
|
||||
formData.append('user1',inputElems[0].id)
|
||||
formData.append('user2',inputElems[1].id)
|
||||
|
||||
|
||||
let params="user1=test&user2=test2"
|
||||
|
||||
xmlhttp.open("POST",'/league/swap')
|
||||
xmlhttp.setRequestHeader('Content-Type', 'application/json')
|
||||
xmlhttp.setRequestHeader('X-CSRF-Token', '{{ csrf_token() }}')
|
||||
|
||||
//xmlhttp.send(params)
|
||||
|
||||
xmlhttp.send(formData)
|
||||
|
||||
return;
|
||||
// For POST, we need a configuration object for the fetch call
|
||||
const update = {
|
||||
title: 'A blog post by Kingsley',
|
||||
body: 'Brilliant post on fetch API',
|
||||
userId: 1,
|
||||
};
|
||||
|
||||
console.log("Before stringify")
|
||||
const options = {
|
||||
method: 'POST',
|
||||
fetch(url, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRF-Token': '{{ csrf_token() }}'
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json, text-plain, */*",
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
"X-CSRF-TOKEN": token
|
||||
},
|
||||
body: JSON.stringify(update),
|
||||
};
|
||||
console.log(JSON.stringify(update))
|
||||
console.log("After stringify")
|
||||
|
||||
fetch('/league/swap', options)
|
||||
.then(data => {
|
||||
if (!data.ok) {
|
||||
throw Error(data.status);
|
||||
}
|
||||
return data.json();
|
||||
}).then(update => {
|
||||
console.log(update);
|
||||
}).catch(e => {
|
||||
console.log("There is some error")
|
||||
alert(e)
|
||||
console.log(e);
|
||||
method: 'post',
|
||||
credentials: "same-origin",
|
||||
body: JSON.stringify({
|
||||
user1: inputElems[0].id,
|
||||
user2: inputElems[1].id
|
||||
})
|
||||
}).then((data) => {
|
||||
window.location.href = redirect;
|
||||
})
|
||||
.catch(function(error) {
|
||||
console.log(error);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
})
|
||||
</script>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue