【问题标题】:How to pass request header to WCF service from cross domain?如何将请求标头从跨域传递给 WCF 服务?
【发布时间】: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();
    }

【问题讨论】:

标签: c# javascript jquery wcf rest


【解决方案1】:

添加 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);
                }

【讨论】:

  • 它仍然为空。你有样品吗?
【解决方案2】:

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

访问控制允许来源:http://foo.example

http://foo.example 是发出请求的站点。您也可以为此使用通配符。 (小心“Access-Control-Allow-Origin: *”!)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-23
    • 2016-04-21
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 2018-09-21
    • 2017-06-01
    • 2017-12-08
    相关资源
    最近更新 更多