How can I get the status code from an http error in Axios?

前端 未结 10 1962
南旧
南旧 2020-11-28 02:27

This may seem stupid, but I\'m trying to get the error data when a request fails in Axios.

axios.get(\'foo.com\')
    .then((response) => {})
    .catch((         


        
10条回答
  •  醉酒成梦
    2020-11-28 02:48

    It's my code: Work for me

     var jsonData = request.body;
        var jsonParsed = JSON.parse(JSON.stringify(jsonData));
    
        // message_body = {
        //   "phone": "5511995001920",
        //   "body": "WhatsApp API on chat-api.com works good"
        // }
    
        axios.post(whatsapp_url, jsonParsed,validateStatus = true)
        .then((res) => {
          // console.log(`statusCode: ${res.statusCode}`)
    
                console.log(res.data)
            console.log(res.status);
    
            // var jsonData = res.body;
            // var jsonParsed = JSON.parse(JSON.stringify(jsonData));
    
            response.json("ok")
        })
        .catch((error) => {
          console.error(error)
            response.json("error")
        })
    

提交回复
热议问题