【发布时间】:2012-07-29 20:40:56
【问题描述】:
从调用方站点我将获得以下代码:
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
beforeSend: function (xhr) {
xhr.setRequestHeader("My-Key", '12345');
},
crossDomain: true,
xhrFields: {
withCredentials: true
},
url: "http://targetsite.com/services/myservice/mymethod",
dataType: "json",
success: function (response) {
},
error: function (message) {
}
});
在目标站点 service 我有以下代码:
public override void ProcessRequest(ref RequestContext requestContext)
{
var keys = (HttpRequestMessageProperty)
requestContext.RequestMessage.Properties[HttpRequestMessageProperty.Name];
string apiKey = prop.Headers["My-Key"];// this is coming null always
}
我得到 apiKey 为空。你能告诉我我在这里做错了什么吗?
编辑:我也在我的目标站点 Global.asax.cs 开始请求事件中尝试了这个,但没有运气:
//Enable Cross Domain WCF Configuration
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
string rqstMethod = HttpContext.Current.Request.Headers["Access-Control-Request-Method"];
if (rqstMethod == "GET" || rqstMethod == "POST" || rqstMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "My-Key,X-Requested-With, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "3628800");
HttpContext.Current.Response.AddHeader("type", "application/json; charset=utf-8");
HttpContext.Current.Response.End();
}
【问题讨论】:
-
您是在改写之前的问题还是只是不耐烦?
-
jsonp 不允许添加自定义请求标头:stackoverflow.com/questions/10546822/…
标签: c# javascript jquery wcf rest