Send object with axios get request [duplicate]

2020-06-30 04:45发布

I want to send a get request with an object. The object data will be used on the server to update session data. But the object doesn't seem to be sent correctly, because if I try to send it back to print it out, I just get:

" N; "

I can do it with jQuery like this and it works:

 $.get('/mysite/public/api/updatecart', { 'product': this.product },  data => {     
              console.log(data);
           });

The object is sent back from server with laravel like this:

public function updateCart(Request $request){
      return serialize($request->product);

The same thing doesn't work with axios:

axios.get('/api/updatecart', { 'product': this.product })       
                .then(response => {
                    console.log(response.data);
            });

I set a default baseURL with axios so the url is different. It reaches the api endpoint correctly and the function returns what was sent in, which was apparently not the object. I only get "N; " as result.

1条回答
冷血范
2楼-- · 2020-06-30 05:46

Axios API is a bit different from the jQuery AJAX one. If you have to pass some params along with GET request, you need to use params property of config object (the second param of .get() method):

axios.get('/api/updatecart', {
  params: {
    product: this.product
  }
}).then(...)
查看更多
登录 后发表回答