ASP.net
Awesome
Learn
Forum
Buy
Demos
Sign In
☾
☀
Switch to
Dark
Light
Mode
this site works best with javascript enabled
Ask Question
how to Pass headers and content type in ajaxdropdown request
Title:
B
I
{code}
?
I am using several ajax drop-downs in my view for eg: @(Html.Awe().AjaxDropdownFor(o => o.SelectUserID) .Url(Url.Action("GetMyTeamList", "DashBoard"))) What ever ajax request is generated based on this , I want to pass some custom headers to that ajax request. How can I achieve this in awesome mvc ? It should be something like:- $.ajax({ type: 'POST', url: '/Home/Ajax', cache: false, headers: { '__RequestVerificationToken': token }, contentType: 'application/json; charset=utf-8', data: { title: 'This is my title', contents: 'These are my contents' }, ... });
Save Changes
Cancel
SalesARM
asked at 28 May 2019
Answers
B
I
{code}
?
`'__RequestVerificationToken'` is already being sent by default, if you have it ( `@Html.AntiForgeryToken()` ) present in the page. You can see the token being attached to the request in `utils.js` after line: `awe.bfr = function (opt) {`. You can also use `.DataFunc(jsFunction)` instead of `.Url(string)` and in the jsFunction you can return the `promise` returned by the `$.ajax()`
Save Changes
Cancel
Omu
answered at 28 May 2019
I don't understand this line "if you have it present in the dom, you can see it being attached in utils.js after line: awe.bfr = function (opt) {" I can see in the chrome dev options, the xhr request is something like:-
at 28 May 2019
SalesARM
Accept: */* Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9,hi;q=0.8 Connection: keep-alive Content-Length: 3 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Cookie: __RequestVerificationToken=sfPZcVS_D9...; ArmLive=username=....; ASP.NET_SessionId=.....; .ASPXAUTH=1758DE6D7045AB7DD88....... DNT: 1 Host: demo.test.com Origin: https://demo.test.com Referer: https://demo.test.com/Dashboard/Consolidated User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36 X-Requested-With: XMLHttpRequest
at 28 May 2019
SalesARM
p ovr void OnAuthorization(AuthorizationContext filterContext) { var request = filterContext.HttpContext.Request; if (request.HttpMethod == WebRequestMethods.Http.Post) { if (request.IsAjaxRequest()) { var antiForgeryCookie = request.Cookies[AntiForgeryConfig.CookieName]; var cookieValue = antiForgeryCookie != null?antiForgeryCookie.Value:null; AntiForgery.Validate(cookieValue, request.Headers["__RequestVerificationToken"]); } else { new ValidateAntiForgeryTokenAttribute().OnAuthorization(filterContext); } } }
at 28 May 2019
SalesARM
AntiForgery.Validate(cookieValue, request.Headers["__RequestVerificationToken"]); This line throws error as is it unable to find the header "__RequestVerificationToken" in the request
at 28 May 2019
SalesARM
Any update regarding this ?
at 28 May 2019
SalesARM
you can download our latest demo, all the requests in it have the `__RequestVerificationToken` set
at 28 May 2019
Omu
please
Sign In
to leave an answer
By accessing this site, you agree to store cookies on your device and disclose information in accordance with our
cookie policy
and
privacy policy
.
OK