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