Search code examples
c#jquerywcf

How To Send Custom Header using Jquery Ajax and Retrieve the same header value in WCF Service using C#?


I try to send header value from Jquery Ajax like this

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

but i can't consume this header value in wcf service using C#..If there is any way to send custom header using ajax and retrieve same header in wcf service

Thanks in advance


Solution

  • Using jQuery's ajax method, I will send headers like this:

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

    In WCF, I will consume headers like this:

    //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();