簡體   English   中英

Axios POST 請求返回代碼上的空數據但在 Insomnia/Postman 工作

[英]Axios POST request returning empty data on code but working at Insomnia/Postman

我在 react native 中遇到 axios 請求的問題,它發送一個帶有名為“cpfcnpj”的 ID 號的帖子,該號碼在 PostgresSQL 中獲取並返回相應的行。 它在 Insomnia 和 Postman 中完美返回行,但在代碼中,response.data 以空數組“[]”的形式出現,但代碼為 200,有人可以幫我嗎? 我很確定這可能是語法錯誤。

(這發生在 localhost 和托管 API 的 heroku 上)

AXIOS 代碼:

      
       await axios({
        method: 'post',       
         headers: {
        "Content-Type": "application/json",
      },
        url: 'https://api-carnedecasa.herokuapp.com/api/login',
        data: {
          cpfcnpj:"15004799793"
        }
      }).then((response) => {
        console.log(response.data);
      }, (error) => {
        console.log(error);
      });; 

節點銀行代碼:


        process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;

        (async () => {
           
            try {
              var res = await pool.query('SELECT * FROM cliente WHERE cpfcnpj = $1', [req.body.cpfcnpj])              
              var data = res.rows
            } finally {
              result.json(data)
             
            }
           
          })().catch(err => console.log(err.stack))
                
          
     }, 

要求在失眠症上正常工作:

在此處輸入圖像描述

非常感謝你們!

解決了我在標題中添加 'Accept': 'application/json' 和 JSON.stringify 正文的問題!

  const rawResponse = await axios(`https://api-carnedecasa.herokuapp.com/api/login`, {
              method: 'POST',
              headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
              },
              data: JSON.stringify({cpfcnpj: "15060655709"})
            }).then(function(response) {
                console.log(response.data);

暫無
暫無

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

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