0

I know I am not the only one with this problem and I have already read many articles on the subject in the last two days. Unfortunately, I have not yet found a working answer for my case. Therefore I try to post my code here and hope for help.

I am trying to add a profile to a list using fetch(). The site is ssl secured.

Here is my code:

const api = "123456789123456789";
const list_id = "LIstId";
const url = `https://a.klaviyo.com/api/v2/list/${list_id}/members?api_key=${api}`;


var myHeaders = new Headers();

myHeaders.append("Accept", "application/json");
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Access-Control-Allow-Origin", "*"); 



var raw = JSON.stringify({
            "profiles": [
                {
                "first_name": "Henry",
                "last_name": "Dust",
                "email": "henry@dust.com"
                }
            ]
            });



var requestOptions = {
                method: 'POST',
                headers: myHeaders,
                body: raw,
                redirect: 'follow'
              };




fetch(url, requestOptions)
          .then(response => response.text())
          .then(result => console.log(result))
          .catch(error => console.log('error', error))

All i get is:

But all i get is:

Access to fetch at 'https://a.klaviyo.com/api/v2/list/ListId/members?api_key=123456789123456789' from origin 'https://liveurl.com' has been blocked by CORS policy

Any help is appreciated, I'm stuck here for 2 days now.

Thank you

I use the code Klavyio gives you in the docs. I tried specific Headers like : "Access-Control-Allow-Origin", "*". I upload the Site to public

Franky
  • 11
  • 2
  • 1
    First thing first: You do not command the server headers. `"Access-Control-Allow-Origin", "*"` is supposed to put on server to say to Browser: "Hey browser, if you find this response data, then you can let any website make use of that content. If it's your server, then go implement CORS in it. If not, there is probably a way in your Configuration Account to make them ADD your Domaine Name in the response they sent back to you, so that the Browser can let you use the response. – KeitelDOG Mar 01 '23 at 19:00

0 Answers0