javascript - 即使正在通过基本身份验证,也会显示输入用户名和密码的警报
问题描述
我正在尝试使用 razorpay 中的订单 API 生成订单 ID。
我的代码:-
此代码在 head 脚本中:-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
下面的代码在正文的脚本中
var jsonData = {
"currency": "INR",
"amount": "10000",
};
$.ajax({
type: 'POST',
dataType: 'jsonp',
url: url,
data: { json: JSON.stringify(jsonData) },
xhrFields: {
withCredentials: true
},
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
},
contentType: "application/json; charset=utf-8",
success: function (response) {
alert("done");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
jsonValue = jQuery.parseJSON(xhr.responseText);
console.log(xhr.Message);
},
});
上面的代码不断向我显示状态代码为 0 的警报框。
解决方案
我试图重现您的输出 [alert with status 0] 我能够做到
然后我开始寻找解决方案,我发现这个答案是一个不错的起点。按照链接,我发现这个答案和另一个非常有用。设置dataType: 'jsonp'
改变了游戏
最终给我 200 回警报的片段如下:
var jsonData = {
"text1" : "textData1",
"number": 10
};
$.ajax({
type: 'POST',
dataType: 'jsonp',
url: 'https://your.endpoint',
//crossDomain: true,
data : { json: JSON.stringify(jsonData) },
contentType: "application/javascript; charset=utf-8",
success: function (response){
alert("done");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
console.log(thrownError.Message);
},
});
由于我们触发了尽管成功,您可能也error
想看看这个答案
我还查看了 RazorPay API [我从未使用过它们]。关于创建订单,我看到它amount
作为整数而不是字符串传递。然后我根据这个答案修改了脚本。顺便说一句,我猜您需要将其乘以 100,因为金额以“货币子单位”表示 [“例如,对于 ₹295 的金额,请输入 29500”]
祝你有美好的一天,
安东尼诺
推荐阅读
- javascript - Javascript,打开/关闭功能
- javascript - 代码不等待 forEach 完成:异步
- asp.net-mvc - 为特定的 Facebook 分享使用 OG 元标记
- xcode - Xcode 9 - 根据目标使用语言
- ruby - Windows 上适用于 Android 的 Calabash - 无法进行第一次测试
- python - Django 1.11 模型迁移操作不适用
- matlab - Octave 的 MATLAB dmod 替代品是什么?
- vhdl - VHDL shift_right 数字
- postgresql - psycopg2 不插入 unicode 数据
- spring - 使用聚合函数的查询方法验证失败