类型错误:无法在回调时读取未定义的属性(读取"数据响应")



我正试图根据您给我的指示查询api,但我得到这个错误:

(index):30 Uncaught TypeError: Cannot read properties of undefined (reading ' datresponse ')在callback ((index):30)在HTMLButtonElement。onclick((指数):23)回调@(指数):30Onclick @ (index):23

api指令https://drive.google.com/file/d/1KHLduw46l1e3aIGCliTvn1gYuJhv8Npy/view?usp=sharing你能帮我一下吗?

<!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(user@test.com) "
data-password="(password 'sha256Hex')"
data-button=false
data-response=true
data-amount="100.01"
data-commerce="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>

看起来你这里的错误是一个简单的用户错误,与API无关。

你正在使用onclick按钮调用callback()"没有传递任何参数。这是一样的:"回调(定义);">

我觉得你的逻辑有点混乱。你的按钮应该调用API, API应该调用回调。相反,你用按钮调用回调,完全跳过API。

通常,错误TypeError: Cannot read properties of undefined指试图在对象或对象的属性中引用不存在的属性中的属性。

问题是,您正在尝试读取属性"描述"。dataResponse,但dataResponse不存在。但是编译器不知道dataResponse是什么,因为变量data不存在。

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

根源就在这里onclick="callback();",您没有在回调中传递任何数据,并试图访问实际上是undefined的函数中的数据。

我不确定这个api,但在你的按钮上点击

  1. 你应该重定向用户到网关页面,这将有两个脚本独立于你的按钮

  1. 你应该在dom中添加上述脚本,点击按钮,它会自动处理回调