前端向后端传递数据用qs库解决问题系列


引入QS库

npm install qs
import qs from 'qs'

问题1:axios 向后端传递数组会默认给数组的每个参数添加一个“[]“,如何解决这个问题?

post请求去掉"[]"

axios.post("",Qs.stringify(this.userIds,{
    
    indices:false})

get请求去掉"[]"

axios.delete("user/deleteUserById", {
    
    
                    params: {
    
    
                        userIds: this.userIds
                    },
                    paramsSerializer: {
    
    
                        serialize: params => {
    
    
                            return Qs.stringify(params, {
    
    indices:false})
                        }
                    }
                })

问题2:服务端只能接收from Date请求,客户端post请求传递数据为pay load请求

QS将pay load请求序列化为from Date请求

  this.$axios
                .post('login', this.$qs.stringify(this.user))
                .then(response=>{
    
    
                    let result=response.data;   
                })
                .catch(error=>{
    
    
                    alert(error)
                })

猜你喜欢

转载自blog.csdn.net/woschengxuyuan/article/details/129225379