I have a weird problem when using jQuery call in my ASP.NET MVC project. I found that the Ajax call gives 404 (resource error not found). But when I use a regular GET URL call, I can successfully call the server without any problems. Any idea why this is?
This ASP.NET MVC Code
public class ViewRecordController: Controller { public JSONResult GetSoftwareChoice(string username) { return Json(username); } }
This is my jQuery code:
$(function() { $("#username").click(function() { $.getJSON("ViewRecord/GetSoftwareChoice", {username:'123'}, function(data) { alert(data); }); }); });
The above JQuery gives me a 404 error. Apparently, ViewRecord/GetSoftwareChoice not found on the server, as for the AJAX call.
But if I print this in my web browser:
http://myapp/ViewRecord/GetSoftwareChoice?username=123
then no problem.
It is very strange.
Just in case you are interested, this is my route:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); }
Edit: I enter my code and find that calling the URL ViewRecord/GetSoftwareChoice?username=123 .
Related question: Select an element inside a form that does not work in jQuery
jquery asp.net-mvc
Graviton
source share