I encountered a peculiar issue with my Web API: it returns a 404 error when called from AJAX, but functions perfectly fine on PostMan and Swagger.
Here is the post method code snippet:
// POST api/<controller>
[HttpPost]
public int Post(string url, string RData, string aid, string AuthToken)
{
//do something
}
Below is the jQuery AJAX call implementation:
function SaveData() {
var $form = $("#formMain");
var data = Serialize($form);
var RData = JSON.stringify(data);
var Url = window.location.href;
var aid = "12332";
//Get Auth Token
$.ajax({
url: "/api/Register/MyAPIKey", //Getting Auth Token first
type: "GET",
success: function (res) {
let AuthToken = res;
//After receiving Auth token, finally submit the registration data. This throws 404 error only on browser.
$.ajax({
type: "POST",
url: '/api/Register',
data: {
'url': Url,
'RData': RData,
'aid': aid,
'AuthToken': AuthToken
},
success: function (data) {
//Show success message
}
});
}
});
}
The error message displayed on the browser console states:
Message: "No HTTP resource was found that matches the request URI 'http://localhost:54318/api/Register'."
MessageDetail: "No action was found on the controller 'Register' that matches the request."
There is also a screenshot from Postman attached for reference showing an expected -1
response even though there may be some validation errors present in the API method. The important thing to note is that the status is 200
.