簡體   English   中英

使用node.js中的axios獲取發布數據

[英]Get data with post using axios in node.js

如何從使用post來獲取數據的api中獲取數據? 這是我的代碼,但是當我記錄數據時api不返回任何內容。

更改服務器支持的Content-Type Qsquerystring庫。

axios.post('https://example.com/api', Qs.stringify({
    'function': 'getEvents'
}), {
    withCredentials: true,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(function(response) {
    console.log(response.data);
}, function(error) {
    console.log(error);
})

jsfiddle中的代碼,但是Access-Control-Allow-Origin也會出錯,您可以代理自己或更改服務器原始訪問權限。

我認為您嘗試作為請求正文傳遞的數據是要通過url編碼的表單數據,因此,請嘗試

axios.post('https://example.com/api?function=getSomething')
.then(function(response) {
      console.log(response.data);
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM