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

2 に答える 2

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);
                }
于 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 に答える