How can I get the header properties from a jquery ajax call. I am sending a code in the header so I need to read it in the webmethods:
$.ajax({
type: "POST",
url: url,
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: success,
error: error,
headers: {
'aaaa': "code"
}
});
On the client-side (I am assuming asmx as you requested the webmethod), you can use the HttpContext.Current to get the current HttpContext. By reading the Request, you can get the headers.
An example to read all the headers would be:
public string GetRequestHeaders()
{
HttpContext ctx = HttpContext.Current;
if (ctx?.Request?.Headers == null)
{
return string.Empty;
}
string headers = string.Empty;
foreach (string header in ctx.Request.Headers.AllKeys)
{
string[] values = ctx.Request.Headers.GetValues(header);
headers += string.Format("{0}: {1}", header, string.Join(",", values));
}
return headers;
}
To read your specific header, you can read the
HttpContext.Current.Request.Headers['aaa']