Home > database >  How to send object for request in Laravel controller from Vue JS frontend by GET request?
How to send object for request in Laravel controller from Vue JS frontend by GET request?

Time:10-08

I have created a function in my controller that receives a request and does some calculations after that and stuff. Currently I am just showing the request array for checking:

public function check_availability(Request $request){
        dd($request->all());
        //other works to use this request values
         
    }

Now when I am sending a request to the route which hits this function using postman like this: enter image description here

So it is working perfectly from postman. But the same request is returning blank request array when I am sending the request from my vue js application.

var data= {
          "qty": 1000,
          "id": 1
        }
        var config = {
          method: 'get',
          url: 'http://127.0.0.1:8000/api/check_quantity',
          data: data
        };

        axios(config)
        .then(function (response) {
          console.log("returned :", response)
          commit('set_products', payload);
        })
        .catch(function (error) {
          console.log("this is error:",error);
        });

This is returning blank array! This is working when I am configuring the whole system in POST method. How can I solve this using get method?

CodePudding user response:

to pass data in get method you have to add them in query params like ?=foo=bar

so your code should like like

var data= {
  qty: 1000,
  id: 1
}
var config = {
  method: 'get',
  url:`http://127.0.0.1:8000/api/check_quantity?qty=${data.qty}&id=${data.id}`,
};

axios(config)
.then(function (response) {
  console.log("returned :", response)
  commit('set_products', payload);
})
.catch(function (error) {
  console.log("this is error:",error);
});

  • Related