How to set a cookie in WCF and read it in the Ajax calling success function (or elsewhere)

I have jQuery JavaScript calling a simple WCF web service. It's pretty easy for me to set a cookie in JavaScript and read it on the server side.

Here is the code.

Client side (JavaScript):

document.cookie = "father=christmas";

Server side (C # in WCF):

var cookieHeader = WebOperationContext.Current.IncomingRequest.Headers[System.Net.HttpRequestHeader.Cookie];
if (!String.IsNullOrEmpty(cookieHeader))
{
    var match = cookieHeader.Split(';').Select(cookie => cookie.Split('=')).FirstOrDefault(kvp => kvp[0] == "father");
    if (match != null)
    {
        result = match[1]; // result now equals "christmas"
    }
}

But I would also like to set a cookie in WCF on the server and read this on the client. Here is my code that cannot do this.

Server side (C # in WCF):

WebOperationContext.Current.OutgoingResponse.Headers[System.Net.HttpResponseHeader.SetCookie] = "cloud=lonely";

Client side (jQuery JavaScript):

$(document).ready(function () {
    $.ajax({
        url: "/ScratchpadSite/Service.svc/Hallo",
        type: "POST",
        success: function (data, textStatus, jqXHR) {
            var xrh = jqXHR.getAllResponseHeaders();
            $('body').html('<p>Father ' + data.d + '<\/p>');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('body').html('<p>' + textStatus + ': ' + errorThrown + '<\/p>');
        }
    });
});

However, the xhr value (the variable I was hoping to contain my "cloud = lonely" cookie)

Server: ASP.NET Development Server / 10.0.0.0
Date: Wed, 04 Apr 2012 15:29:27 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 17
Cache-control: private
Content-Type: application / json; charset = utf-8
Connection: Close

(N.B. - ( JavaScript) WCF , .)

cookie ? , , ? , ?

+2
3

, , . Jfriend00 , W3 getAllResponseHeaders() , Set-Cookie. Dan Tao , (?) cookie , , , ! "- setTimeout ".

( ), Drew Marsh "WCF → ASMX Cookies" - WCF.

- (, ), WCF , , , , ( cookie) WCF, :

WebOperationContext.Current.OutgoingResponse.Headers.Add("cloud", "lonely");

, JavaScript/jQuery/Ajax cookie, :

$(document).ready(function () {
    $.ajax({
        url: "/ScratchpadSite/Service.svc/Hallo",
        type: "POST",
        success: function (data, textStatus, jqXHR) {
            $('body').html('<p>Father ' + data.d + '<\/p>');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('body').html('<p>' + textStatus + ': ' + errorThrown + '<\/p>');
        },
        complete: function (jqXHR, textStatus) {
            var cloudsFeeling = jqXHR.getResponseHeader("cloud");
            if (cloudsFeeling) {
                document.cookie = "cloud=" + cloudsFeeling;
            }
        },
    });
});

HTTPResponseHeader , , , , , , .

0

cookie $. cookie, , cookie

, .

0

All cookies set earlier in the same domain that you are sending the HTTP request should automatically be sent to your request, in other words, all cookies are automatically sent in the HTTP request, so that the domain to which the HTTP request is made is set what cookie before. in response, if you want to set a cookie in the client, you can add a response header with the name "Set-Cookie"

0
source

Source: https://habr.com/ru/post/1758980/


All Articles