gpt4 book ai didi

javascript - 从浏览器中的对象获取数据

转载 作者:行者123 更新时间:2023-11-28 16:51:32 24 4
gpt4 key购买 nike

如何从浏览器获取对象/从浏览器获取对象中存储的数据?

  1. 用户在填写了错误信用卡号的表单后按下提交按钮(我正在测试的场景),这会触发 jQuery 函数 SubmitAPI()(代码如下)。
  2. Google Chrome 控制台显示 400 Bad Request 错误(因为信用卡号不正确)以及包含被拒绝信用卡数据的对象的 API 响应(响应如下)
  3. 我特别需要获取"response_type":"D","re​​sponse_code":"U20","re​​sponse_desc":"INVALID CREDIT CARD NUMBER",因为我想将此错误消息显示给用户。我怎样才能在 jQuery 中做到这一点?
  4. 我已经尝试了几个小时来解决这个问题。当交易成功(批准的信用卡)时,我使用 response.response.response_type 来获取响应类型。然而,如果信用卡号码错误,同样的尝试会导致“未定义”。因此,我只想从我的 Google Chrome 浏览器中获取已成功获取响应代码响应的数据。

第 1 部分:jQuery 代码(直接来自 API 文档 - 除非我将信用卡更改为错误号码)

  function SubmitAPI() {
var settings = {
"url":
"https://sandbox.forte.net/api/v3/organizations/org_ID/locations/loc_ID/transactions",
"method": "POST",
"headers": {
"X-Forte-Auth-Organization-Id": "org_ID",
"Authorization": "ID",
"Content-Type": "application/json"
},
"data": JSON.stringify({ "action": "sale", "authorization_amount": 102.45, "subtotal_amount": 99.95, "billing_address": { "first_name": "Jennifer", "last_name": "McFly" }, "card": { "card_type": "visa", "name_on_card": "Jennifer McFly", "account_number": "41111sdf11111111", "expire_month": "12", "expire_year": "2017", "card_verification_value": "123" } }),
};

$.ajax(settings).always(function (response) {
console.log(response);
});
}

第 2 部分:控制台响应:

400 (Bad Request)

第 3 部分:浏览器中的响应对象:

{"location_id":"loc_241789","action":"sale","authorization_amount":102.45,"entered_by":"59ae172b3bd78bed493ecd5892975764","billing_address":{"first_name":"Jennifer","last_name":"McFly"},"card":{"name_on_card":"Jennifer McFly","last_4_account_number":"1111","masked_account_number":"****1111","expire_month":12,"expire_year":2017,"card_type":"visa"},"response":{"environment":"sandbox","response_type":"D","response_code":"U20","response_desc":"INVALID CREDIT CARD NUMBER"}}

最佳答案

使用 Ajax 调用的错误处理程序,并且能够毫无问题地获取错误消息。

var settings = {
"url": "https://sandbox.forte.net/api/v3/organizations/org_381529/locations/loc_241789/transactions",
"method": "POST",
"headers": {
"X-Forte-Auth-Organization-Id": "org_381529",
"Authorization": "Basic NTlhZTE3MmIzYmQ3OGJlZDQ5M2VjZDU4OTI5NzU3NjQ6ZWUwZTZiZDA4ZThlMWNhNWQ3MzUyNGU0ZWU5ZDFjNTg=",
"Content-Type": "application/json"
},
"data": JSON.stringify({
"action": "sale",
"authorization_amount": 102.45,
"subtotal_amount": 99.95,
"billing_address": {
"first_name": "Jennifer",
"last_name": "McFly"
},
"card": {
"card_type": "visa",
"name_on_card": "Jennifer McFly",
"account_number": "41111sdf11111111",
"expire_month": "12",
"expire_year": "2017",
"card_verification_value": "123"
}
}),
};

$.ajax(settings).error(function(xhr) {
console.log("Error", xhr.responseJSON.response.response_desc);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

关于javascript - 从浏览器中的对象获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59887199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com