I have a weird problem when using JQuery call in my ASP.NET MVC project. I found that the Ajax call gives 404 ( resource not found error). But when I use the usual URL GET c
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);
});
});
});
Replace the equal sign with a colon:
$(function() {
$("#username").click(function() {
$.getJSON("ViewRecord/GetSoftwareChoice", {username:'123'},
function(data) {
alert(data);
});
});
});
Use Firefox Firebug add on, and watch what request gets made by Jquery...
Is it possible that the page that this Jquery runs in is in a subdirectory, in which case the request will not be relative root as the http://myapp/ "typed in" url is?
Also, I am guessing the code you specified above is not actually the code you are using (which is completely reasonable, I rairly post code as-is). Because
$.getJSON("ViewRecord/GetSoftwareChoice", {username='123'},
the = sign between username and '123' is invalid JS as far as I know. So I'm betting there is some goofy detail in the the real code that is causing the problem.
Old function :
function Chart() {
var url = "../Home/Pie/";
$.ajax({
url: url,
data: {},
cache: false,
type: "POST",
success: function (data) {
var chartData = data;
createChart(chartData);
$(document).bind("kendo:skinChange", createChart);
},
error: function (xhr, status, error) {
$('#alertdialog').html(status);
$('#alertdialog').dialog('open');
return false;
}
});
}
Answers : var url = "Home/Pie/"; Removed ../ from url
$(function() {
$("#username").click(function() {
$.getJSON('<%= Url.Action("GetSoftwareChoice", "ViewRecord")%>',{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'},