【问题标题】:How To Send Custom Header using Jquery Ajax and Retrieve the same header value in WCF Service using C#?如何使用 Jquery Ajax 发送自定义标头并使用 C# 在 WCF 服务中检索相同的标头值?
【发布时间】:2016-06-22 05:38:39
【问题描述】:

我尝试像这样从 Jquery Ajax 发送标头值

$.ajaxSetup({
    url:http://my url
    headers: { "CustomHeader": "myValue" }
});

但我不能使用 C# 在 wcf 服务中使用此标头值。如果有任何方法可以使用 ajax 发送自定义标头并在 wcf 服务中检索相同的标头

提前致谢

【问题讨论】:

  • 它应该可以工作。您如何访问您的标题。
  • 字符串消息 = OperationContext.Current.RequestContext.RequestMessage.ToString();
  • 阅读此链接可能会对您有所帮助:Read Request Header in wcf

标签: c# jquery wcf


【解决方案1】:

使用 jQuery 的ajax 方法,我将发送这样的标题:

$.ajax({       
    url: varUrl, // Location of the service  
    headers: {
        "Test": "Test-Value", // "Header-name" : "Header-value"          
    }       
});

在 WCF 中,我将使用这样的标头:

//retrieve your header value using "Header-name"...    
WebOperationContext current = WebOperationContext.Current;
WebHeaderCollection headers = current.IncomingRequest.Headers;
string Test= current.IncomingRequest.Headers.GetValues("Test")[0].ToString();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 2015-12-09
    相关资源
    最近更新 更多