如何从跨域将请求标头传递给WCF服务?

时间:2012-07-29 20:40:57

标签: c# javascript jquery wcf rest

从来电者网站我将获得此代码:

$.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 null。你能让我知道我在这里做错了吗?

编辑:我也在我的目标网站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();
    }

2 个答案:

答案 0 :(得分:0)

使用添加requestHeader

的跨域调用JQuery $ .ajax示例
function TestingWCFRestWithJsonp() {
                    $.ajax({
                        url: "http://targetsite.com/services/myservice/mymethod",
                        dataType: "jsonp",
                        type: "GET",
                        beforeSend: function(xhr) { 
                                         xhr.setRequestHeader("My-Key", '12345');
                                   },
                        timeout: 10000,
                        jsonpCallback: "MyCallback",
                        success: function (data, textStatus, jqXHR) {
                            alert(data);
                        },
                        error: function (jqXHR, textStatus, errorThrown) {alert('error');

                        },
                        complete: function (jqXHR, textStatus) {alert('complete');
                        }
                    });
                }
                function MyCallback(data) {
                    alert(data);
                }

答案 1 :(得分:0)

在您的服务上,尝试在响应中添加Access-Control-Allow-Origin标头。它指定允许哪些站点对您的服务进行跨域调用。

Access-Control-Allow-Origin:http://foo.example

http://foo.example是发出请求的网站。您也可以使用通配符。 (注意" Access-Control-Allow-Origin:*"!)