JQuery Ajax调用提供404’资源未找到’错误,但普通URL调用很好

在我的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错误。 显然,就AJAX调用而言,在服务器上找不到ViewRecord/GetSoftwareChoice

但是,如果我在我的网络浏览器中输入:

 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

相关问题: 选择Form中的元素不能在JQuery中工作

您可能想尝试使用UrlHelper,而不是对url进行硬编码:

 $(function() { $("#username").click(function() { var url = '<%= UrlHelper.Action("GetSoftwareChoice", "ViewRecord") %>'; $.getJSON(url, {username: '123'}, function(data) { alert(data); }); }); }); 

我通过使用FireBug向我展示由JQuery生成的请求来解决此问题。 令我惊讶的是,生成的url是

 http://localhost/ViewRecord/ViewRecord/GetSoftwareChoice?username=123 

对于JSON调用:

 $(function() { $("#username").click(function() { $.getJSON("ViewRecord/GetSoftwareChoice", {username:'123'}, function(data) { alert(data); }); }); }); 

所以我只需将$.getJSON行更改为

 $.getJSON("GetSoftwareChoice", {username:'123'}, 

或者,使用正斜杠:

  $.getJSON("/ViewRecord/GetSoftwareChoice", {username:'123'}, 
 $(function() { $("#username").click(function() { $.getJSON('<%= Url.Action("GetSoftwareChoice", "ViewRecord")%>',{username: '123'}, function(data) { alert(data); }); }); }); 

使用Firefox Firebug添加,并观察Jquery发出的请求…

这个Jquery运行的页面是否可能在子目录中,在这种情况下,请求将不是相对root,因为http:// myapp / “in type in”url是?

另外,我猜你上面指定的代码实际上并不是你正在使用的代码(这是完全合理的,我按照原样发布代码)。 因为

 $.getJSON("ViewRecord/GetSoftwareChoice", {username='123'}, 

据我所知,用户名和’123’之间的=符号是无效的JS。 所以我打赌在导致问题的真实代码中有一些愚蠢的细节。

用冒号替换等号:

 $(function() { $("#username").click(function() { $.getJSON("ViewRecord/GetSoftwareChoice", {username:'123'}, function(data) { alert(data); }); }); }); 

我也有同样的问题。 在Graviton完成后使用Firebug后,我看到我的路径很糟糕所以我把它改成了我的行动名称。 Get_OrderLine是我在控制器中操作的名称。 inv_item_id是传递给控制器​​操作的参数。

 // Update OrderLine data by returning a JSON result $('#itemsddl').click(function (e) { var selectedItem = $(this).val(); var actionURL = "Get_OrderLine"; var d = "inv_item_id=" + selectedItem; var uom = $('#uom'); var size = $('#size'); var unitLbs = $('#unitLbs'); var totalLbs = $('#totalLbs'); var shipName = $('#shipName'); var hazardClass = $('#hazardClass'); var unnaNo = $('#unnaNo'); var packingGroup = $('#packingGroup'); var placard = $('#placard'); var ergNo = $('#ergNo'); $.ajax({ cache: false, type: 'GET', url: actionURL, data: d, datatype: JSON, success: function (data) { uom.val(data.uom); size.val(data.size); unitLbs.val(data.unitLbs); totalLbs.val(data.totalLbs); shipName.val(data.shipName); hazardClass.val(data.hazardClass); unnaNo.val(data.unnaNo); packingGroup.val(data.packingGroup); placard.val(data.placard); ergNo.val(data.ergNo); }, error: function (xhr, ajaxOptions, thrownError) { alert('Failed to query item - ' + thrownError + "\n" + "Full details: " + xhr.responseText); } }); e.preventDefault(); }); 

这是我的动作,它将JSON结果返回给我的jQuery。 然后jQuery函数处理从JSON到HTML的映射。 不是很漂亮,但它有效。

 public ActionResult Get_OrderLine(int? inv_item_id) { HazmatInfoItem item = new HazmatInfoItem(); item.itemId = "0"; item.size = "0"; item.unitLbs = 0; item.qty = 0; item.totalLbs = item.qty * item.unitLbs; item.shipName = ""; item.hazardClass = ""; item.unnaNo = ""; item.packingGroup = ""; item.placard = ""; item.ergNo = ""; var items = from i in hazmatRepository.GetAllItems() select i; // Get item details items = items.Where(i => i.INV_ITEM_ID.Contains(inv_item_id.ToString())); foreach (var i in items) { item.uom = i.UNIT_MEASURE_STD; item.size = i.INV_ITEM_SIZE; item.unitLbs = 1; item.totalLbs = item.unitLbs * item.qty; item.shipName = i.PAG_SHIPPING_NAME; item.hazardClass = i.HAZ_CLASS_CD; item.unnaNo = i.MSDS_ID; item.packingGroup = i.PACKING_CD; item.placard = i.PAG_PLACARD_TYPE; } return Json(item, JsonRequestBehavior.AllowGet); } 

我最初添加了一个尝试处理此问题的新路由,但将其注释掉以允许默认路由。

希望这些解决方案可以帮助那些在尝试将.ajax与jQuery和MVC一起使用时遇到类似问题的人。

旧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; } }); } 

答案: var url =“Home / Pie / ”; 从url中删除../