Skip to main content
Solved

AJAX subscription issues

  • May 9, 2023
  • 1 reply
  • 499 views

Forum|alt.badge.img+1

Hi there. We're new to Klaviyo (migrating over from Mailchimp) and are creating a custom sign-up form via AJAX.

 

However, when doing so we are facing a CORS policy issue, with a console error of 'Access to XMLHttpRequest at 'https://a.klaviyo.com/api/v2/list/XXXX/subscribe' from origin 'https://devsite.local' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.'

 

Is there any way around this, by adding accepted URLs for example to our Klaviyo account?

 

If not, any pointers as to the best AJAX based approach would be much appreciated.

 

Here is our current code:

 

$.ajax({

  type: "POST",

  url: "https://a.klaviyo.com/api/v2/list/{list}/subscribe",

  data: JSON.stringify({

    api_key: "{api-key}",

    email: email,

  }),

  cache: false,

  dataType: "json",

  contentType: "application/json; charset=utf-8",

  // Success

  success: function (response) {

    // Output result for debugging

    console.log(response);

  },

  // Error

  error: function (response) {

    // Output result for debugging

    console.log(response);

  },

});

 

Many thanks

Best answer by David To

Hello @kaleidografik,

Welcome to Klaviyo and the Community!

If you haven’t already, I would suggest taking a look at some past Community posts on this subject. There’s been several posts whose solution has been helpful to others in the past. I’ve included them below:

 

I also noticed in your call that you’re referencing our v2 endpoint. Since our v1 and v2 endpoints are in the process of being depreciated, you may want to look into switching to the new version of our API. You can learn the difference of the two from our Comparison between v1/v2 and new APIs Developer guide.

 I hope this helps!

David

View original
Did this topic or the replies in the thread help you find an answer to your question?

1 reply

David To
Klaviyo Employee
Forum|alt.badge.img+60
  • Klaviyo Employee
  • 2456 replies
  • Answer
  • May 9, 2023

Hello @kaleidografik,

Welcome to Klaviyo and the Community!

If you haven’t already, I would suggest taking a look at some past Community posts on this subject. There’s been several posts whose solution has been helpful to others in the past. I’ve included them below:

 

I also noticed in your call that you’re referencing our v2 endpoint. Since our v1 and v2 endpoints are in the process of being depreciated, you may want to look into switching to the new version of our API. You can learn the difference of the two from our Comparison between v1/v2 and new APIs Developer guide.

 I hope this helps!

David