首页 > 解决方案 > 即使正在通过基本身份验证,也会显示输入用户名和密码的警报

问题描述

我正在尝试使用 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 的警报框。

标签: javascripthtmljqueryrestrazorpay

解决方案


我试图重现您的输出 [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”]

祝你有美好的一天,
安东尼诺


推荐阅读