Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST Requests with axios not sending parameters

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);
                })
        },
like image 270
Praveesh Avatar asked Nov 01 '17 13:11

Praveesh


People also ask

How send data in Axios put request?

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' });

How pass parameters to Axios get method?

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.

Is Axios post asynchronous?

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.

How do I send a POST request from Axios?

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() .


1 Answers

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);
}
like image 51
Дмитрий Павлов Avatar answered Oct 31 '22 14:10

Дмитрий Павлов