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

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”的作用

发布了62 篇原创文章 · 获赞 33 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zjbyough/article/details/103570747