简体   繁体   English

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

[英]How to pass request header to WCF service from cross domain?

From the caller site I will be having this code: 从呼叫者站点,我将获得以下代码:

$.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) {
    }
});

In the target site service I have the following code: 在目标站点服务中,我具有以下代码:

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
}

I am getting apiKey null. 我得到apiKey null。 Can you let me know what I am doing wrong here? 您能告诉我我在做什么错吗?

EDIT: I tried with this too in my target site Global.asax.cs Begin request event but no luck: 编辑:我也在目标站点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();
    }

Sample JQuery $.ajax call for cross domain with adding a 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);
                }

On your service, try adding a Access-Control-Allow-Origin header to the response. 在您的服务上,尝试将Access-Control-Allow-Origin标头添加到响应中。 It specifies which sites are allowed to make cross-domain calls to your service. 它指定允许哪些站点对您的服务进行跨域调用。

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

Where http://foo.example is the site making the request. 其中http://foo.example是发出请求的网站。 You can also use wildcards for this. 您也可以为此使用通配符。 (Be careful with "Access-Control-Allow-Origin: *" !) (请小心使用“ Access-Control-Allow-Origin:*”!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM