使用axios发送对象获取请求 [英] Send object with axios get request

查看:870
本文介绍了使用axios发送对象获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送一个带有对象的get请求.对象数据将在服务器上用于更新会话数据.但是该对象似乎没有正确发送,因为如果我尝试将其发送回以打印出来,我将得到:

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; "

我可以像这样用jQuery来做到这一点,并且有效:

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

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

对象通过laravel从服务器发送回,如下所示:

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

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

同一件事不适用于axios:

The same thing doesn't work with axios:

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

我使用axios设置了默认的baseURL,因此该URL是不同的.它正确到达api端点,并且该函数返回发送的内容,这显然不是对象.结果我只得到" N; ".

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.

推荐答案

Axios API 与jQuery AJAX有点不同.如果必须随GET请求一起传递一些参数,则需要使用config对象的params属性(.get()方法的第二个参数):

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

这篇关于使用axios发送对象获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆