簡體   English   中英

MVC Controller 中的 HttpPost 未通過 Ajax POST 調用

[英]HttpPost in MVC Controller not being called through Ajax POST

我有一個 controller(信息技術控制器)。 controller 包含一個動作(LocationChangeRequest)。 該操作采用可選參數(id)。

public ActionResult LocationChangeRequest(ChangeRequestType id = ChangeRequestType.WithinDepartment)

該操作返回包含當前 model 數據的視圖。

return View(locationChangeRequest);

在該視圖中,有一個 function 執行 ajax 帖子(代碼如下)來搜索員工信息。

員工搜索1

到達該視圖的 url 是:

http:// [not relavant here] /InformationTechnology/LocationChangeRequest

當用戶嘗試使用路由參數訪問該視圖時,員工搜索 function 不會執行。

url到達視圖的路由參數為:

http:// [not relavant here] /InformationTechnology/LocationChangeRequest/1

我發現的是在路徑中使用 /1 參數時沒有命中 InformationTechnology controller 中的 HttpPost 方法。 雖然它似乎與路徑中的參數有關,但我似乎無法弄清楚如何解決這個問題。

任何有關如何使用參數通過 url 處理 HttpPost 的建議將不勝感激。

HttpPost 代碼如下:

[HttpPost]
public JsonResult SearchUser(string term)
   {
     ...
     return Json(results, JsonRequestBehavior.AllowGet);
   }

javascript代碼如下:

 $.ajax({ url: searchUserUrl, type: "POST", dataType: "json", data: { term: request.term }, success: function (data) { response($.map(data, function (item) { return { label: item.Name, value: item.HexKey }; })); }, error: function (xhr, error) { console.debug(xhr); console.debug(error);} })

您需要為使用 $ 定義一個全局變量,如下所示。

var $=jQuery.noConflict();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM