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);
})
},
Sending a PUT Request with Axios The simplest way to make the PUT call is to simply use the put() function of the axios instance, and supply the body of that request in the form of a JavaScript object: const res = await axios. put('/api/article/123', { title: 'Making PUT Requests with Axios', status: 'published' });
In this section, we will learn how to make Axios GET requests with query parameters. First, add the following code to the index. js file: // Axios GET Query Parameters const url = require("url"); const queryParams = { limit: 1, sort: "desc", }; const params = new url.
Axios is a promise based HTTP client for the browser and Node. js. Axios makes it easy to send asynchronous HTTP requests to REST endpoints and perform CRUD operations. It can be used in plain JavaScript or with a library such as Vue or React.
A POST request can be made using Axios to “post” data to an endpoint. This endpoint may then use this POST request to perform a certain task or trigger an event. The HTTP post request is performed by calling axios. post() .
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With