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

时间:2017-05-04 01:20:37

标签: javascript c# asp.net ajax asp.net-mvc

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

Structure of files][1

代码用于在运行时填充表,方法是调用相关的控制器来获取代码并使用脚本将其打印出来。

我的控制器:

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是引用SQL数据库的edmx模型的名称。

当我调试应用程序时,我收到404未找到错误。这会发生在网址:&#39;&lt;%= Url.Action(&#34; GetAllPeople&#34;,&#34; HomeController&#34;)%&gt;&#39;和url:&#39; / Controllers / HomeController / GetAllPeople&#39;,以及url:&#39; @ Url.Action(&#34; /HomeController.cs/GetAllPeople")。

有什么明显的东西我不见了吗?请记住,我在回答时对AJAX和Javascript非常陌生。

2 个答案:

答案 0 :(得分:2)

这是一个有效的演示:https://dotnetfiddle.net/kE5Px7

我刚做了一处更改,将HttpPost属性添加到您正在呼叫的操作中。

[HttpPost]
public JsonResult GetAllPeople()

并按照评论中的建议,操作返回:

return Json(people);

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

答案 1 :(得分:0)

您只需要进行一项更改,即将HomeController更改为Home。我们在通话中仅使用控制器名称。例如&#39; / Home / GetAllPeople&#39; 。编写&#39; / Controllers / HomeController / GetAllPeople&#39;是错误的。