2016-09-22 76 views
0

我想通過javascript(ReactJS)向Wit.ai發出API請求。我的網絡瀏覽器選項卡顯示呼叫失敗消息:Wit.ai客戶端API調用失敗

「錯誤」:「壞身份驗證,檢查令牌/ PARAMS」

然而,相同的呼叫顯示在Wit.ai一樣成功日誌。我已驗證憑證是否正確,並且可以通過終端成功撥打電話。

這裏的電話:

async action() { 
    const resp = await fetch('https://api.wit.ai/message?v=20160526&q=hello', { 
     method: 'GET', 
     headers: { 
     'Authorization': "Bearer " + accessToken 
     }, 
     dataType: 'jsonp', 
     mode: 'no-cors', 
     credentials: 'include' 
    }).then(resp => resp.json()).catch(e => console.log('Boo', e)); 
} 

回答

0

因爲它是一個JSONP請求時,你會被其「輸入意外結束」即使該請求已經被正確執行。如果不通過應用服務器代理請求,我不確定還有什麼方法可以使其工作。不管怎麼說,對於這種要求,徹底清除headers和移動訪問令牌進入查詢字符串作爲access_token查詢參數:

await fetch(`https://api.wit.ai/message?v=20160526&q=hello&access_token=${accessToken}`, { 
    method: 'GET', 
    dataType: 'jsonp', 
    mode: 'no-cors', 
    credentials: 'include' 
}).then(resp => resp.json()).catch(e => console.log('Boo', e)); 

退房瀏覽器上的「網絡」選項卡,看看該請求已得到圓滿解決,甚至儘管在執行此fetch調用期間已達到catch塊。