在 ASP.NET MVC 项目中使用 JQuery 调用时,我遇到了一个奇怪的问题。我发现 Ajax 调用给出 404(资源未找到错误)。但是当我使用通常的 URL GET 调用时,我可以成功调用服务器,没有任何问题。知道为什么会这样吗?
这是我的 ASP.NET MVC 代码
public class ViewRecordController: Controller
{
public JSONResult GetSoftwareChoice(string username)
{
return Json(username);
}
}
这是我的 JQuery 代码:
$(function() {
$("#username").click(function() {
$.getJSON("ViewRecord/GetSoftwareChoice", {username:'123'},
function(data) {
alert(data);
});
});
});
上面的 JQuery 给我一个 404 错误。显然是ViewRecord/GetSoftwareChoice
就 AJAX 调用而言,在服务器上找不到。
但如果我在网络浏览器中输入:
http://myapp/ViewRecord/GetSoftwareChoice?username=123
那么就没有问题了。
这确实很奇怪。
如果您有兴趣,这是我的路线:
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
.
相关问题:选择表单内的元素在 JQuery 中不起作用