简体   繁体   中英

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.

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

Hope this would help.

You can simply get QueryString paramaters as arguments in your Controller:

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

You can also use Request.QueryString to get 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.

Now you would fetch those value in action method like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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