简体   繁体   English

MVC Controller 中的 HttpPost 未通过 Ajax POST 调用

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

I have a controller (InformationTechnologyController).我有一个 controller(信息技术控制器)。 That controller contains an action (LocationChangeRequest). controller 包含一个动作(LocationChangeRequest)。 That action takes an optional parameter(id).该操作采用可选参数(id)。

public ActionResult LocationChangeRequest(ChangeRequestType id = ChangeRequestType.WithinDepartment)

That action returns a view with the current model data.该操作返回包含当前 model 数据的视图。

return View(locationChangeRequest);

Within that view, there's a function that performs an ajax post (code below) to search for employee information.在该视图中,有一个 function 执行 ajax 帖子(代码如下)来搜索员工信息。

Employee Search 1员工搜索1

The url to reach that view is:到达该视图的 url 是:

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

When a user attempts to reach that view using a route parameter, the Employee Search function does not perform.当用户尝试使用路由参数访问该视图时,员工搜索 function 不会执行。

The url to reach the view with the routing parameter is: url到达视图的路由参数为:

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

What I discovered is the HttpPost method in the InformationTechnology controller is not being hit when using the /1 parameter in the path.我发现的是在路径中使用 /1 参数时没有命中 InformationTechnology controller 中的 HttpPost 方法。 Athough it seems to have to do with the parameter in the path, I can't seem to figure out how to solve the problem.虽然它似乎与路径中的参数有关,但我似乎无法弄清楚如何解决这个问题。

Any advice on how to handle hitting the HttpPost through the url with the parameter would be appreciated.任何有关如何使用参数通过 url 处理 HttpPost 的建议将不胜感激。

The HttpPost code is as follows: HttpPost 代码如下:

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

The javascript code is as follows: 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);} })

You need to define a global variable for using the $ as below.您需要为使用 $ 定义一个全局变量,如下所示。

var $=jQuery.noConflict(); var $=jQuery.noConflict();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM