0

I am really confused on this problem. Hope you could give me some hints!

First, there is a test.html which host on client server and with a button. Then, the below ajax code is called when button is clicked. A new window will be popped out.

test.html

$.ajax({
    crossDomain: true,
    xhrFields: {
        withCredentials: true
    }, 
    type: 'POST',
    url: 'a.com/a.aspx',   //the server file
    data: 'data=sth',
    contentType: "application/x-www-form-urlencoded; charset=utf-8",
    dataType: "text",
    success: function (data) {
            if (win != null) {
                if (win.opener == null) {
                    win.opener = self;
                }
                win.location.href = data;  //return the url of website B

                if (win.focus) win.focus();
            }

        },
        error: function (data) {
            alert("");

        }
    });

After that, the return data would navigate the new window to website B.

For cors ajax call, I have added

crossDomain: true,
    xhrFields: {
        withCredentials: true
    }

to the test.html and

    <add name="Access-Control-Allow-Origin" value="//client domain" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Method" value="POST,OPTIONS"/>
    <add name="Access-Control-Allow-Credentials" value="true"/>

to the server's web.config

The problem is, in the new window, the cookies is reset after navigating to website B.

Updates

win.location.href = 'a.com';  // it works
win.location.href = other domain; // cookies is reset

I found that win.location.href makes session lost with cross domain. How to deal with it?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Chris
  • 11
  • 4
  • I have observed that, when setting win.location.href to another domain, the cookies lost. – Chris Apr 28 '20 at 09:42
  • Does this help with what you are trying to do? https://stackoverflow.com/questions/3342140/cross-domain-cookies – mattbloke Apr 28 '20 at 10:10
  • @mattbloke I followed the suggestion for making cors ajax call and it works. However, I cannot navigate the window to another domain. 1. win.location.href = 'a.com' (success, cookie kept) 2. win.location.href = 'c.com' (fail, cookie is reset) – Chris Apr 28 '20 at 10:15

0 Answers0