CORS Error on adding contact

Hello, I’m Trying to Add a contact using the API.

I can only do it in javascript because I don’t have server side access.

SO I put that:

$.ajax({
            method: 'POST',
            url: "https://bagatelles-lingeriecom.api-us1.com/admin/api.php",
            crossDomain: true,
            data: {
                'api_action': 'contact_sync',
                'api_key': '_key',
                'email': $('#compte_email').val(),
                'field[%CIVILIT%,0]': $('#compte_civilite').val(),
                'first_name': $('#compte_prenom').val(),
                'last_name': $('#compte_nom').val(),
                'p[1]': 1,
                'status[1]': 1,
                'sdate[1]': _date
            }
        }
        );

but when submitting, I get a No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Error (I use Chrome)

What’s wrong with my request?

Best regards.

1 Like

The API really wasn’t designed to allow you to make client-side requests like this, since it forces you to expose your API key and that’s a vulnerability.

There are a few other endpoints (that are admittedly poorly documented) that allow you to add a contact.

Try making a POST request to the form action from one of your forms, thats how I have accomplished this in the past. I think there is another one, I will keep digging around and try and find it.

Otherwise, you could spin up a quick node server w/ express and heroku and proxy the request. That has worked well for other customers in the past – and will keep your API key secure.