我在客户网站上有一张worldpay表格,如果不在Safari上禁用防止跨站点跟踪功能,它将无法在ipad上使用。
它似乎与跨站点Cookie有关。

该表格如下所示:

<div class="d-none">
    <form action="https://secure.worldpay.com/wcc/purchase" target="payment-iframe" method="post" id="worldpayForm">
        <input type="hidden" id="instId" name="instId">
        <input type="hidden" id="cartId" name="cartId">
        <input type="hidden" id="amount" name="amount">
        <input type="hidden" id="currency" name="currency">
        <input type="submit" id="makePayment" value="Buy This ">
    </form>
</div>

<iframe name="payment-iframe" style="min-width: 760px; min-height: 450px; border: 0;"></iframe>


在JavaScript中,有一行可以做到这一点:

$('#worldpayForm').submit();


如果我注释掉该行,则不会收到镶边警告。
我读过某处,您可以添加以下内容:

response.setHeader("Set-Cookie", "HttpOnly;Secure;SameSite=Strict");


但这似乎只是在谈论向其他域发出请求。我该如何应用呢?

最佳答案

我设法解决了这个问题。是的,我确实有自己的cookie解决方案:

export function getCookie(name) {
    var i, x, y;
    const arCookies = document.cookie.split(';');
    for (i = 0; i < arCookies.length; i++) {
        x = arCookies[i].substr(0, arCookies[i].indexOf('='));
        y = arCookies[i].substr(arCookies[i].indexOf('=') + 1);
        x = x.replace(/^\s+|\s+$/g, '');
        if (x === name) {
            return unescape(y);
        }
    }
    return '';
}

export function setCookie(name, value, expiresInDays) {
    const expiryDate = new Date();
    expiryDate.setDate(expiryDate.getDate() + expiresInDays);
    const cookieValue = escape(value) + (expiresInDays === null ? '' : `; expires=${expiryDate.toUTCString()}`);
    document.cookie = `${name}=${cookieValue}`;
}

export function clearCookies() {
    const cookies = document.cookie.split(';');

    for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i];
        const eqPos = cookie.indexOf('=');
        const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;`;
    }
}


我刚刚将其更新为:

export function getCookie(name) {
    var i, x, y;
    const arCookies = document.cookie.split(';');
    for (i = 0; i < arCookies.length; i++) {
        x = arCookies[i].substr(0, arCookies[i].indexOf('='));
        y = arCookies[i].substr(arCookies[i].indexOf('=') + 1);
        x = x.replace(/^\s+|\s+$/g, '');
        if (x === name) {
            return unescape(y);
        }
    }
    return '';
}

export function setCookie(name, value, expiresInDays) {
    const expiryDate = new Date();
    expiryDate.setDate(expiryDate.getDate() + expiresInDays);
    const cookieValue = escape(value) + (expiresInDays === null ? '' : `; expires=${expiryDate.toUTCString()}`);
    document.cookie = `${name}=${cookieValue};SameSite=Strict;secure`;
}

export function clearCookies() {
    const cookies = document.cookie.split(';');

    for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i];
        const eqPos = cookie.indexOf('=');
        const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;SameSite=Strict;secure`;
    }
}


现在一切正常。

10-06 04:45