I am trying to POST some data from Vue.js to a backend based on Symfony using the following code.
updateQuestion : function() {
axios.post('/staff/question/api/' + this.id,{
id : 'test',
name : 'sree'
})
.then( response => {
console.log(response);
})
.catch(error => {
console.log(error);
})
},
However, the parameters that I am attaching to the POST request are not reaching my controller. So, I tried the alternate format for POST requests and still, the parameters are not reaching the controller. Please tell me what's wrong.
Alternate format:
updateQuestion : function() {
axios({
method : 'POST',
url : '/staff/question/api/' + this.id,
data: {
id : 'test',
name : 'sree'
}
})
.then( response => {
console.log(response);
})
.catch(error => {
console.log(error);
})
},
解决方案
I also encountered this problem!
My post data was found in the controller:
$request->getContent();
My vue script
onSubmit() {
axios.post('/test/post/data', { test: "test" })
.then(response => {
console.log(response.data);
});
},
My Symfony controller:
public function postData(Request $request)
{
$data = $request->getContent();
$data = json_decode($data, true);
return $this->json($data);
}