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#,
我想在此请求标头中添加一个变量并在C#上使用它,
I try many ways but I can't consume it on C#:
我尝试了很多方法,但我不能在C#上使用它:
beforeSend: function (req) { req.setRequestHeader("AgentGUID", this.AgentGUID); },
Pass
parameters:
beforeSend:function(req) { req.setRequestHeader(“AgentGUID”,this.AgentGUID); },
Can you help me? I don't want to change the function at the C# part I just want to use something like:
你可以帮我吗?我不想改变C#部分的功能我只想使用类似的东西:
(System.Web.HttpContext.Current.Request.Headers["someHeader"]
1 个解决方案
#1
4
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).
你的beforeSend应该可以按你的意愿工作,但是你没有在服务器端得到这个值的原因是这个方法调用的this.AgentGUID是未定义的,因为在这个上下文中它指向另一个对象(很可能是ajax请求对象)。
By defining a variable outside your ajax call you issue will be fixed.
通过在ajax调用之外定义变量,您将解决问题。
var me = this;
var response = $.ajax({
...
beforeSend: function (req)
{
req.setRequestHeader("AgentGUID", me.AgentGUID);
},
...
});
#1
4
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).
你的beforeSend应该可以按你的意愿工作,但是你没有在服务器端得到这个值的原因是这个方法调用的this.AgentGUID是未定义的,因为在这个上下文中它指向另一个对象(很可能是ajax请求对象)。
By defining a variable outside your ajax call you issue will be fixed.
通过在ajax调用之外定义变量,您将解决问题。
var me = this;
var response = $.ajax({
...
beforeSend: function (req)
{
req.setRequestHeader("AgentGUID", me.AgentGUID);
},
...
});