Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

javascript - API returns an error with a wrong API path

I have the following JavaScript code:

$(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");
        }
      });
    }
  });
});

When I run it, I receive the following error in the console:

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

The issue is the API call should look like https://localhost:44367/api/customers/5.

How do I fix this issue?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Because it is taking relative path. Only add '/' in the beginning of your url.

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

It should take you to https://localhost:44367/api/customers/5 instead of https://localhost:44367/Customers/api/customers/5


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...