从来电者站点我将获得以下代码:
$.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) {
}
});
在目标站点服务中,我有以下代码:
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();
}