AJAX 404错误 - 调用控制器操作.Net MVC。

3

我有一个相对简单的.NET应用程序,只有一个页面。

文件结构

当调用相关控制器以获取代码并使用脚本打印出来时,代码旨在在运行时填充表格。

我的控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using techTest3.Models;  

namespace techTest3.Controllers
{
    public class HomeController : Controller
    {
    public ActionResult Index()
    {
        return View();
    }

    public JsonResult GetAllPeople()
    {
        TechTestEntities testTechObj = new TechTestEntities();
        var people = testTechObj.People.Select(x => new
        {
            Id = x.PersonId,
            Name = x.FirstName,
            Surname = x.LastName,
            Valid = x.Var1,
            Authorised = x.Var2
        }).ToList();
        return Json(people, JsonRequestBehavior.AllowGet);
    }  

}
}

我的观点:

@{
ViewBag.Title = " Showing Data Using jQuery Ajax Call JSON in ASP.NET MVC";
} 
<h1>Showing Data Using jQuery Ajax Call JSON in ASP.NET MVC </h1> 

<div>
    <table id = "tblEmployee" class = "tblEmployee" >
    <thead>
    <!---<img src = "~/Loading.gif" alt = "Loading" id = "imgLoading" class = "Load" />-->
    </thead> 

        <tbody>
         </tbody> 
        </table> 
        </div> 

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script type = "text/javascript" >
    $(document).ready(function()
    {
        $("#tblEmployeetbodytr").remove();
        $.ajax
        ({
            type: 'POST',
            url: '@Url.Action("GetAllPeople")',
            dataType: 'json',
            data: {},
            success: function(data)
            {
            $("#imgLoading").hide();
            var items = '';
            var rows = "<tr>" +  
                    "<th align='left' class='EmployeeTableTH'>Employee ID</th><th align='left' class='EmployeeTableTH'>Name</th><th align='left' class='EmployeeTableTH'>Project Name</th><th align='left' class='EmployeeTableTH'>Manager Name</th><th align='left' class='EmployeeTableTH'>City</th>" +  
                    "</tr>";  
            $('#tblEmployeetbody'

).append(rows);

        $.each(data, function(i, item)  
        {  
            var rows = "<tr>" +  
                "<td class='EmployeeTableTD'>" + item.Id + "</td>" +  
                "<td class='EmployeeTableTD'>" + item.FirstName + "</td>" +  
                "<td class='EmployeeTableTD'>" + item.LastName + "</td>" +  
                "<td class='EmployeeTableTD'>" + item.Var1 + "</td>" +  
                "<td class='EmployeeTableTD'>" + item.Var2 + "</td>" +  
                "</tr>";  
            $('#tblEmployeetbody').append(rows);  
        });  
        },
        error: function(ex)
        {
        var r = jQuery.parseJSON(response.responseText);
        alert("Message: " + r.Message);
        }
        });
        return false;
        }); </script> 
        <style type = "text/css" >
        </style> 

TechTestEntities是一个edmx模型的名称,该模型引用了一个SQL数据库。
当我调试应用程序时,出现404未找到错误。这种情况发生在url为'<%= Url.Action("GetAllPeople", "HomeController") %>'、'/Controllers/HomeController/GetAllPeople'和'@Url.Action("/HomeController.cs/GetAllPeople")'时。
是否有什么显而易见的问题我忽略了?请注意,我对AJAX和Javascript非常陌生。

2
你需要使用[HttpPost]修饰GetAllPeople操作,否则它只会响应GET请求。 - Maria Ines Parnisari
2
在 helpers @Url.Action("GetAllPeople", "Home") 中删除单词“Controller”。 - Jasen
1
此外,由于 JSON 控制器被标记为 HttpPost 并返回 JsonResult,因此请删除 JsonRequestBehavior.AllowGet 并改为使用 return Json(people) - Tetsuya Yamamoto
浏览器调试工具中显示的 URL 是什么?它会显示请求的完整 URL。 - Jasen
这是404页面的URL:[localhostnumber]/Controllers/HomeController/GetAllPeople。这是当前页面的URL:http://localhost:49810/Home/index。请求是否发送到了错误的位置? - peanut
显示剩余2条评论
3个回答

3
这是一个工作演示: https://dotnetfiddle.net/kE5Px7 我只做了一个更改,将 HttpPost 属性添加到您调用的操作中。
[HttpPost]
public JsonResult GetAllPeople()

如评论中所建议的,该操作返回以下内容:

return Json(people);

AJAX调用的URL保持不变:url: '@Url.Action("GetAllPeople")'

我还在遇到问题,但是我会看一下这个例子的,谢谢。 - peanut
你在代码中分享的视图是 Index.cshtml 吗? - Maria Ines Parnisari
是的,没错。这就是启动页面。 - peanut
2
谢谢,搞定了!问题出在需要正确调用写入函数、确保要添加的表与类名匹配以及确保字段名称正确。 - peanut

2

您只需要做一个更改,就是将HomeController更改为Home。我们在调用时只使用控制器名称。例如'/Home/GetAllPeople'。写成'/Controllers/HomeController/GetAllPeople'是错误的。


0
我发现了这篇帖子,但是里面的解决方案并不能解决我的问题。对于我来说,AJAX调用找不到控制器。之前这个调用是有效的,但有一天它突然停止工作了。我修改了控制器以添加新的方法并重命名了AJAX调用,但仍然没有效果。
对我唯一真正起作用的解决方案是创建一个新的控制器,复制所有的方法并重新命名AJAX调用。在Visual Studio和编译版本中出了一些问题,但我不知道是什么原因。我只是想发布这种情况,以防有人要花费数小时来调试这个问题。祝你编写愉快!

参考视频就是我遇到的情况:https://www.youtube.com/watch?v=we1_aL1KYiE


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接