2017-08-13 39 views
0

所以我使用axios爲了發送一個JSON請求到Django API。Django API請求是空的使用React

axios({ 
    method: 'post', 
    url: 'http://127.0.0.1:8000/' + this.state.dataRequestEndPoint, 
    data: (reqData) 
}); 

之前的Axios公司撥打我可以看到請求:

Object {nvm: "", a: Array(1), b: Array(1), c: Array(1), d: Array(1)} 

然而,當它到達的Django:

class TargetClass(APIView): 
    def get(self, request): 
    Here request is empty: 
     (Pdb) request.data 
     <QueryDict: {}> 
     (Pdb) request.body 
     b'' 
    def post(self): 
     pass 

我在做什麼錯?

P.S.試圖發送請求以及fetch以及:

fetch('http://127.0.0.1:8000/' + this.state.dataRequestEndPoint, { 
    method: 'POST', 
    body: reqData, 
}) 

它都不工作。

+0

你是否檢查過它是如何在瀏覽器的網絡部分發送的?就像任何瀏覽器的開發者工具一樣。 – Fawaz

+1

您是否嘗試過使用API​​測試工具(如郵遞員或其他)的端點? –

+0

在networks選項卡中看起來沒問題。我還沒有嘗試過任何工具,因爲我認爲當我設法從url中擊中它應該沒問題?問題是我需要傳遞這些參數才能返回適當的數據。 – VnC

回答

1

這是上述問題的解決方案。顯然,愛可信需要發送參數:

var reqData = this.state.requestParams 
    axios({ 
    method: 'post', 
    url: 'http://127.0.0.1:8000/' + this.state.dataRequestEndPoint, 
    params: { 
     'a': reqData['a'], 
     'b': reqData['b'] 
    } 
    }) 
    .then(function (response) { 
    console.log(response) 
    }) 
    .catch(function (error) { 
    console.log(error) 
    }); 

不過,我不知道這個解決方案如何好是安全的,明智的。

+0

爲什麼做你提到的不安全?這是因爲如果params? – CatherineAlv