Skip to main content

Hello,

Any idea why this simple script is working fine on Safari but not in Chrome/Firefox?

The HTTP status is always the same 202 though, supposedly success.

 

Thank for your help!

 

const options = {
    method: "POST",
    mode: "cors",
    headers: {
        revision: "2024-10-15.pre",
        "content-type": "application/vnd.api+json"
    },
    body: JSON.stringify({
        data: {
            type: "subscription",
            attributes: {
                profile: {
                    data: {
                        type: "profile",
                        attributes: {
                            email: "sarah2.mason@klaviyo-demo.com",
                            subscriptions: {
                                email: {
                                    marketing: {
                                        consent: "SUBSCRIBED"
                                    }
                                }
                            }
                        }
                    }
                }
            },
            relationships: {
                list: {
                    data: {
                        type: "list",
                        id: "xxx"
                    }
                }
            }
        }
    })
};

fetch("https://a.klaviyo.com/client/subscriptions?company_id=xxx", options)
    .then((response) => {
        console.log("HTTP Status:", response.status, response.statusText);
        if (!response.ok) {
            throw new Error(
                `Request failed: ${response.status} - ${response.statusText}`
            );
        }
        return response.text();
    })
    .then((text) => {
        console.log("Raw Response Text:", text);
        if (text) {
            const json = JSON.parse(text);
            console.log("Parsed Response JSON:", json);
        } else {
            console.log("No response body received.");
        }
    })
    .catch((error) => {
        console.error("Fetch error:", error.message);
    });
 

Hi youness, when you say it’s not working, what exactly is or isn’t appearing in the account that should be? Additionally, do you see logs of the attempted subscribe in the Developer Logs of the account?

If you’re not seeing profiles, or not seeing consent change after subscribing, it could be due to the list that is being subscribed to and it’s opt-in setting. If it is DOI, you won’t see the update until the DOI email is clicked. For a demo profile like in your payload, the profile would stay as is forever (since no one is using the demo account emails).

Let me know if there is any additional info about this!


Hi Christian, thanks for the feedback.

It’s not a DOI issue, this code is working just fine on Safari, looks like Chrome is behaving differently.

I’ve spent 2 days trying multiple configurations with your other endpoints, every time it’s working as expected on Safari or via Postman but no chance on Chrome... Looks like probably some CORS issue (even if I read all the topics here about it), but unfortunately nothing I could on my end unless you have some snippet that has been proof tested working on Chrome, I would highly appreciate that!

Thanks!


Reply