JQuery Ajax call gives 404 'Resource Not Found' Error but normal URL call is fine
Instead of hard-coding the URL, you might want to try a UrlHelper:
$(function() {
$("#username").click(function() {
var url = '<%= UrlHelper.Action("GetSoftwareChoice", "ViewRecord") %>';
$.getJSON(url, {username: '123'}, function(data) {
alert(data);
});
});
});
I fix this problem by using FireBug to show me the request that was generated by JQuery. To my amazement, the url generated is
http://localhost/ViewRecord/ViewRecord/GetSoftwareChoice?username=123
for the JSON call:
$(function() {
$("#username").click(function() {
$.getJSON("ViewRecord/GetSoftwareChoice", {username:'123'},
function(data) {
alert(data);
});
});
});
So I just have to change the $.getJSON
line to
$.getJSON("GetSoftwareChoice", {username:'123'},
Alternatively, use the forward slash:
$.getJSON("/ViewRecord/GetSoftwareChoice", {username:'123'},
$(function() {
$("#username").click(function() {
$.getJSON('<%= Url.Action("GetSoftwareChoice", "ViewRecord")%>',{username: '123'}, function(data) {
alert(data);
});
});
});