在我的ASP.NET MVC项目中使用
JQuery调用时,我遇到了一个奇怪的问题.我发现Ajax调用给出404(资源未找到错误).但是当我使用通常的URL GET调用时,我可以毫无问题地成功调用服务器.知道为什么会这样吗?
public class ViewRecordController: Controller { public JSONResult GetSoftwareChoice(string username) { return Json(username); } }
$(function() { $("#username").click(function() { $.getJSON("ViewRecord/GetSoftwareChoice",{username:'123'},function(data) { alert(data); }); }); });
上面的JQuery给了我一个404错误.显然,就AJAX调用而言,在服务器上找不到ViewRecord / GetSoftwareChoice.@H_502_3@
但是,如果我在我的网络浏览器中输入:@H_502_3@
http://myapp/ViewRecord/GetSoftwareChoice?username=123
那没有问题.@H_502_3@
事实上,这非常奇怪.@H_502_3@
如果您有兴趣,这是我的路线:@H_502_3@
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 ); }
编辑:我进入我的代码,发现URL调用是ViewRecord / GetSoftwareChoice?username = 123.@H_502_3@
相关问题:Select Element inside Form not working in JQuery@H_502_3@
解决方法
您可能想尝试使用UrlHelper,而不是对网址进行硬编码:
$(function() { $("#username").click(function() { var url = '<%= UrlHelper.Action("GetSoftwareChoice","ViewRecord") %>'; $.getJSON(url,{username: '123'},function(data) { alert(data); }); }); });