1

从来电者站点我将获得以下代码:

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

2 回答 2

0

示例 JQuery $.ajax 调用跨域并添加 requestHeader

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);
                }
于 2012-07-30T09:10:01.167 回答
0

On your service, try adding a Access-Control-Allow-Origin header to the response. It specifies which sites are allowed to make cross-domain calls to your service.

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

Where http://foo.example is the site making the request. You can also use wildcards for this. (Be careful with "Access-Control-Allow-Origin: *" !)

于 2012-07-30T14:32:12.413 回答