detail":"JSON parse error - Expecting value: line 1 column 1 (char 0)

瘦欲@ 提交于 2019-12-17 01:37:35

detail":"JSON parse error - Expecting value: line 1 column 1 (char 0)

在调用接口时返回400错误,详情是

{detail":"JSON parse error - Expecting value: line 1 column 1 (char 0)"}

原因是传送数据的格式有问题,不要使用, contentType: 'application/json'就好了

发送Ajax请求用的是reqwest,原来的代码是

 postBill() {
            let self = this;
            let token = getQueryVariable('token');
            this.userToken = token;
            let addFamily = 0;
            if(self.is_add_to_family)
                addFamily = 1;
            if (token) {
                reqwest({
                    url: 'http://127.0.0.1:8000/api/v1/bill/Income/?token=' + token
                    , method: "POST"
                    , type: 'json'
                    , contentType: 'application/json'
                    , data: {
                        bill_type: self.bill_type,
                        money: self.money,
                        is_add_to_family: self.is_add_to_family,
                        remarks: self.remarks,
                        time: self.time,
                        concrete_type: self.concrete_type
                    }
                    , success: function (resp) {
                        //
                    }
                })
            }
        },

后来改为

postBill() {
            let self = this;
            let token = getQueryVariable('token');
            this.userToken = token;
            let addFamily = 0;
            if(self.is_add_to_family)
                addFamily = 1;
            if (token) {
                reqwest({
                      url: 'http://127.0.0.1:8000/api/v1/bill/Income/?token=' + token
                    , method: "POST"
                    , type: 'json'
                    , data: {bill_type: self.bill_type,
                        money: self.money,
                        is_add_to_family: self.is_add_to_family,
                        remarks: self.remarks,
                        time: self.time,
                        concrete_type: self.concrete_type
                    }
                    , success: function (resp) {
                        qwery('#content').html(resp)
                    }
                })
            }
        },

具体就是删了, contentType: 'application/json',反正是解决了

原因可以看 ajax 发送json数据时为什么需要设置contentType: "application/json”ajax中设置contentType: “application/json”的作用

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!