微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

API 返回错误的 API 路径

如何解决API 返回错误的 API 路径

我有以下 JavaScript 代码

$(document).ready(function () {
  $("#customers .js-delete").on("click",function () {
    if (confirm("Are you sure you want to delete this customer?")) {
      $.ajax({
        url: "api/customers/" + $(this).attr("data-customer-id"),method: "DELETE",success: function () {
          console.log("Success");
        }
      });
    }
  });
});

当我运行它时,我在控制台中收到以下错误

jquery-3.5.1.js:10099 DELETE https://localhost:44367/Customers/api/customers/5 404

问题是 API 调用应该类似于 https://localhost:44367/api/customers/5

我该如何解决这个问题?

解决方法

因为它走的是相对路径。仅在网址开头添加“/”。

url: "/api/customers/" + $(this).attr("data-customer-id"),

它应该带你到 https://localhost:44367/api/customers/5 而不是 https://localhost:44367/Customers/api/customers/5

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。