简体   繁体   English

从请求正文获取ajax发布的数据

[英]get ajax posted Data From Request body

如何从请求正文中获取这些ajax发布的数据?

search=title&searchType=department+1&X-Requested-With=XMLHttpRequest

You can use [FromBody] attribute to the parameter that you need to get from the body. 您可以将[FromBody]属性用于需要从正文中获取的参数。

Public ActionResult ([FromBody] string param1)
{
    return View();
}

Hope this would help. 希望这会有所帮助。

You can simply get QueryString paramaters as arguments in your Controller: 您只需在控制器中将QueryString参数作为参数即可:

Public ActionResult Index(string search, string searchType,string XRequestedWith)
{
    return View();
}

Of course this way your paramater can not have dashes like X-Requested-With 当然,这样您的参数不能像X-Requested-With这样的破折号

You can also use Request.QueryString to get QueryString. 您也可以使用Request.QueryString来获取QueryString。 This way you won't have the restriction above. 这样,您将没有上述限制。

If you want to get those ajax posted data from request body the first instruction is not to put those values in query string put those values in form body.Better it would be if you create a modal and use those property to pass in ajax. 如果要从请求主体获取那些用ajax发布的数据,那么第一条指令是不要将这些值放入查询字符串中,而是将这些值放入表单主体中,最好是创建一个模式并使用这些属性来传递ajax。

Now you would fetch those value in action method like 现在您将在操作方法中获取这些值,例如

Public ActionResult YourMethodName([FromBody]YourModel objYourModel)
{
   // use objYourModel to fetch your values
   return View();
}

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

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