最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Cannot read properties of undefined (reading 'dataResponse') at callback - Stack Overflo

programmeradmin2浏览0评论

I am trying to query an api according to the instructions you gave me, but I am getting this error:

(index):30 Uncaught TypeError: Cannot read properties of undefined (reading 'dataResponse') at callback ((index):30) at HTMLButtonElement.onclick ((index):23) callback @ (index):30 onclick @ (index):23

api instructions could you help me please

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

      <!-- Bootstrap CSS -->

    <title>Duke Business School</title>
    <script
    data-user="user([email protected]) "
    data-password="(password 'sha256Hex')"
    data-button=false
    data-response=true
    data-amount="100.01"
    data-merce="Affinitas"
    src=":9020/js/transactions.js">
   </script> 
  </head>
  <body>

        <button onclick="callback();"> pagar</button>
      </div>
    </section>

   <script>
    function callback(data){
    alert("RESPUESTA OBTENIDA EN HTML");
    alert("Operación: " + data.dataResponse.description + " Autorización: " +
   data.dataResponse.authorization)
    }
   </script>
  </body>
</html>

I am trying to query an api according to the instructions you gave me, but I am getting this error:

(index):30 Uncaught TypeError: Cannot read properties of undefined (reading 'dataResponse') at callback ((index):30) at HTMLButtonElement.onclick ((index):23) callback @ (index):30 onclick @ (index):23

api instructions https://drive.google./file/d/1KHLduw46l1e3aIGCliTvn1gYuJhv8Npy/view?usp=sharing could you help me please

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

      <!-- Bootstrap CSS -->

    <title>Duke Business School</title>
    <script
    data-user="user([email protected]) "
    data-password="(password 'sha256Hex')"
    data-button=false
    data-response=true
    data-amount="100.01"
    data-merce="Affinitas"
    src="http://52.22.36.22:9020/js/transactions.js">
   </script> 
  </head>
  <body>

        <button onclick="callback();"> pagar</button>
      </div>
    </section>

   <script>
    function callback(data){
    alert("RESPUESTA OBTENIDA EN HTML");
    alert("Operación: " + data.dataResponse.description + " Autorización: " +
   data.dataResponse.authorization)
    }
   </script>
  </body>
</html>

Share Improve this question asked Oct 12, 2021 at 1:44 Maynor CastroMaynor Castro 911 gold badge3 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

It looks like your error here is a simple user error, and has nothing to do with the API.

You're using the button onclick to call "callback()" without any passed parameters. This is the same as: "callback(undefined);"

I believe your logic is a little messy here. Your button should call the API, and the API should call the callback. Instead, you're calling the callback with the button, skipping the API altogether.

Typically, the error TypeError: Cannot read properties of undefined refers to trying to reference a property within a property within an object or object's property that doesn't exist.

The problem is, you're trying to read the property "description" of dataResponse, but dataResponse doesn't exist. But the piler doesn't know what dataResponse is because the variable data doesn't exist.

data.dataResponse.description // <- Same as: Undefined.undefined.undefined

Root cause is here onclick="callback();", you are not passing any data in callback and trying to access data in your function which is actually undefined.

I am not sure about this api but on your button click

  1. you should redirecting user to the gateway page which will have both the scripts independent of your button

or

  1. you should append the mentioned script in dom on button click and it will automatically take care of callback

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论