衫裤跑路 2019-12-10 08:53 采纳率: 50%
浏览 55

Axios / Ajax发布数据问题

I’m trying to learn react so I’m rebuilding the magento2 minicart in react, all seems to be working well apart from I’m having issues submitting the form data if the quantity changes - I’m trying to do this like so:

onChange = (e) => {
    this.setState({ qty: e.target.value }, () => {
        axios({
            headers: {
                'Content-Type': 'application/json'
            },
            method: 'post',
            url: '/checkout/sidebar/updateItemQty/',
            data: {
                item_id: 13,
                item_qty: this.state.qty,
                form_key: 'KTC30XGNGahjfVmn'
            }
        });
    });
}


On a refresh of the page the basket quantity doesn’t update, when checking how default mangento2 posts the information it seems to be different as you can see on the images - how can I change my code above so it matches the default post if that’s the issue?



My axios post: enter image description here

Default M2 post: enter image description here

Update: changing params to data makes the request 302 and changes the type to HTML and can't seem to get it back to json

  • 写回答

2条回答 默认 最新

  • weixin_33720186 2019-12-10 09:00
    关注

    params is to send data in query parameter. To send data in post body use body option. Like this:

    Edit

    Since you are submitting form data, you need to specify content type as application/x-www-form-urlencoded and also need to change way how we send data.

             axios({
                    method: 'post',
                    url: '/checkout/sidebar/updateItemQty/',
                    headers: {
                         'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    data: queryString.stringify({
                        item_id: 9,
                        item_qty: this.state.qty,
                        form_key: 'KTC30XGNGahjfVmn'
                    })
                });
             });
    

    You can get query string module using from here https://github.com/Gozala/querystring#readme

    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作