c# - 如何将请求 header 从跨域传递到WCF服务?

标签 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 为空。你能让我知道我在这里做错了什么吗?

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

最佳答案

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

关于c# - 如何将请求 header 从跨域传递到WCF服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11712766/

相关文章:

c# - 从后台线程启动时,UI 线程上的任务继续

jquery - wysihtml5 对齐方式不正确

c# - 如何在 Windows 应用商店应用程序中添加 GetFileFromPathAsync 的路径

c# - 使用 JavaScript 为 MVC 应用构建相对 URL

javascript - 是否存在 IsCallable 为假但 IsConstructor 为真的 JS 对象?

javascript - jquery load() 等效于离线使用

javascript - 将 4 个字段与 bootstrap 对齐

javascript - 单击 DIV 内的 IFRAME 时删除 DIV 元素

jquery - each() 切换复选框 append 此值 else if 语句在 jQuery

从 PDF 文件中删除文本的 C# 解决方案