I am facing an issue with my controller class named HomeController
and the client-side JavaScript code:
function upload(content) {
var ajax = new XMLHttpRequest();
ajax.open("POST", 'UploadImage', false);
ajax.setRequestHeader('Content-Type', 'application/upload');
ajax.send(content);
}
Sometimes, when using the URL
https://localhost/MyApp/Home/UploadImage
, the data uploads correctly. However, at other times it fails due to the absence of the controller name in the URL. The error shown in Chrome developer tools is:
POST https://localhost/MyApp/UploadImage 404 ()
By manually adding /Home to the URL in the code:
ajax.open("POST", 'Home/UploadImage', false);
The behavior changes unpredictably - sometimes successfully using
https://localhost/MyApp/Home/UploadImage
and other times failing with:
POST https://localhost/MyApp/Home/Home/UploadImage 404 ()
Recompiling and restarting may switch this behavior, but I cannot determine a consistent pattern. This inconsistency also occurs with GET requests. Whether to include the controller name Home/
in URLs perplexes me. How can I ensure consistent behavior?