How to pass parameter from Ajax to C# using header request?
ajax, c#, header
Solution
Your `beforeSend` should work as you wish, but the reason you are not getting the value on server side is that `this.AgentGUID` on this method call is `undefined` because `this` in that context is pointing to another object (most probably ajax request object).
By defining a variable outside your ajax call you issue will be fixed.
var me = this;
var response = $.ajax({
...
beforeSend: function (req)
{
req.setRequestHeader("AgentGUID", me.AgentGUID);
},
...
});
Problem
I have the following request: ``` var response = $.ajax({ type: "POST", contentType: "application/x-www-form-urlencoded", url: this.AgentServiceUrl + "/" + methodName, data: data, async: this.Async, success: function (xml, textStatus) { if (successHandler != null) successHandler(state, $.xml2json(xml), textStatus); }, error: function (xmlHttpRequest, textStatus, errorThrown) { if (errorHandler != null) errorHandler(state, xmlHttpRequest, textStatus, errorThrown); } }); ``` I want to add to a variable to this request header and consume it on C#, I try many ways but I can't consume it on C#: ``` beforeSend: function (req) { req.setRequestHeader("AgentGUID", this.AgentGUID); }, ``` Pass `parameters:` Can you help me? I don't want to change the function at the C# part I just want to use something like: ``` (System.Web.HttpContext.Current.Request.Headers["someHeader"] ```