Home > Mobile >  Write in a <p> api response
Write in a <p> api response

Time:06-23

I'm trying to get a response from this api : https://spen.tk/api/v1/isScamLink

The api response looks like this :

{"status":200,"result":true,"linkFound":""}

I want to get the result part into a var and use it in a paragraph in HTML.

This is my js code in the site :

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
  $(function() {
  $.ajax({
    url: 'https://spen.tk/api/v1/isScamLink?link='   document.getElementById("box").value,
    type: 'GET',
    dataType: 'json',
    success: function(response) {
      var result = response.data.total;
      var final = $('#result').text(result);
    }
  })
})
</script>

It sais this error : Uncaught TypeError: Cannot read properties of undefined (reading 'total')

CodePudding user response:

Your data will be in response.result

  $.ajax({
    url: 'https://spen.tk/api/v1/isScamLink?link=https://google.com',
    type: 'GET',
    dataType: 'json',
    success: function(response) {
      console.log(response.result)
    }
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

CodePudding user response:

Your response json doesn't contain the key 'data', that's why you're getting error.

  • Related