2017-05-04 103 views
2

我有一個相當簡單的.NET應用程序,只有一個頁面。AJAX 404錯誤 - 調用控制器動作.Net MVC

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找不到錯誤。這發生在url:'<%= Url.Action(「GetAllPeople」,「HomeController」)%>'和url:'/ Controllers/HomeController/GetAllPeople',還有url:'@ Url.Action(「/ HomeController的.cs/GetAllPeople「)。

有什麼明顯的我失蹤了嗎?請記住我在回答時對AJAX和Javascript非常陌生。

+2

您需要用'[HttpPost]'裝飾'GetAllPeople'動作,否則它只會響應GET請求 –

+1

在幫助程序中放下單詞「Controller」@ Url.Action(「GetAllPeople」,「Home」 )' – Jasen

+1

此外,由於標記爲'HttpPost'的JSON控制器返回'JsonResult',所以刪除'JsonRequestBehavior.AllowGet'和'return Json(people)'。 –

回答

2

這裏的工作演示:https://dotnetfiddle.net/kE5Px7

我只是一個變化,增加了HttpPost屬性你打電話的動作。

[HttpPost] 
public JsonResult GetAllPeople() 

,並作爲意見提出,動作返回此:

return Json(people); 

AJAX調用的URL是一樣的:url: '@Url.Action("GetAllPeople")'

+0

仍然有我的問題,但我會看看這個例子,謝謝。 – peanut

+0

您在代碼中共享的視圖是「Index.cshtml」嗎? –

+0

是的,它是。這是啓動頁面 – peanut

0

只需要做一個更改就是將HomeController更改爲Home。我們在通話中只使用控制器名稱。例如'/ Home/GetAllPeople'。寫'/ Controllers/HomeController/GetAllPeople'是錯誤的。