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"]
Your
beforeSendshould work as you wish, but the reason you are not getting the value on server side is thatthis.AgentGUIDon this method call isundefinedbecausethisin 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.