cancel
Showing results for 
Search instead for 
Did you mean: 

using ajax to access API

SOLVED
Occasional Contributor

using ajax to access API

I keep getting this error when trying to access the api endpoints via Ajax

 

 

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://dev.mysite.com' is therefore not allowed access.

 

10 REPLIES
Honored Contributor

Re: using ajax to access API

Hello,

 

Unfortunately it is not currently possible to use the Constant Contact API with AJAX because of XSS (cross-site scripting) restrictions. Currently the Constant Contact API does not support JSONP or CORS, and as such it can currently only accessed from the back-end of a website.

 

If the primary use of your integration is to add/update contacts using a form, then you may be able to accomplish this by using our custom sign-up form system. You can find more info on this here: http://developer.constantcontact.com/docs/signup-forms-tools/custom-signup-forms.html

 

If you have any questions, please let me know!

 

Sincerely,

Elijah G.
API Support Engineer
Occasional Contributor

Re: using ajax to access API

HI, Thanks for the reply.

 

I do have 2 questions

Using the php sdk: https://github.com/constantcontact/php-sdk

 

1. How do I get a list of events for a given access token: i.e.: https://api.constantcontact.com/v2/eventspot/events

 

2. How do I get a list of registrants given an event id and access token i.e: https://api.constantcontact.com/v2/eventspot/events/{eventId}/registrants

 

Using the SDK mentioned above.

Honored Contributor

Re: using ajax to access API

Hello,

 

Unfortunately the PHP SDK does not have any methods implemented for our Eventspot endpoints in the API. If you're looking to simply get data about events so that you can display that data, here is a quick snippet of code that uses PHP cURL and will convert the API response to an array of objects containing event data:

 

    $apiKey = "API_KEY";
    $accessToken = "ACCESS_TOKEN";
    $headers = Array("Authorization: Bearer ".$accessToken);
    $url = "https://api.constantcontact.com/v2/eventspot/events?api_key=".$apiKey;
    $events = json_decode(doRequest($url,$headers,"GET"));
// $events now contains either an object representing an API error, or an array of event objects.

// To get a list of registrants, you can do the following:
$eventID = "EVENT_ID";
$url = "https://api.constantcontact.com/v2/eventspot/events/".$eventID."/registrants?api_key=".$apiKey;
$registrants = json_decode(doRequest($url,$headers,"GET"));
// $registrants now contains either an object representing an API error, or an array of registrant objects.
function doRequest($url, $headers, $method, $data=null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_USERAGENT, "Custom cURL Client"); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); if ($data) { curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } $response = new stdclass(); $response->body = curl_exec($curl); $response->info = curl_getinfo($curl); $response->error = curl_error($curl); curl_close($curl); return $response; }

Hopefully this helps you to get up and running with the API! Please let me know if you have any questions!

 

Sincerely,

Elijah G.
API Support Engineer
Occasional Participant

Re: using ajax to access API

@Elijah_G Thank you so much, you saved my life :)

Honored Contributor

Re: using ajax to access API

I'm glad to hear that this was able to help. If you have any questions, please feel free to ask!

 

Sincerely

Elijah G.
API Support Engineer
Occasional Participant

Re: using ajax to access API

Hi Elijah G,

I need your help. When i go to this link: https://constantcontact.mashery.com/io-docs i see a method to send email (Email Campaign Test Send method: /emailmarketing/campaigns/campaignId/tests). when i send email , i always see this line at the bottom : 

THIS IS A TEST EMAIL ONLY.
This email was sent by the author for the sole purpose of testing a draft message. If you believe you have received the message in error, please contact the author by replying to this message. Constant Contact takes reports of abuse very seriously. If you wish to report abuse, please forward this message to abuse@constantcontact.com.

 

How to remove it? or do we have similar method to send email?

Thanks and best regards,

Khoa Pham

Honored Contributor

Re: using ajax to access API

Hello,

 

The endpoint that you are using is explicitly for limited distribution of a test version of an email campaign rather than a live send. In order to do a live send, there are two steps:

  1. Create an email campaign with a POST to /v2/emailmarketing/campaigns - http://developer.constantcontact.com/docs/email-campaigns/email-campaigns-collection.html?method=POS...
  2. Create a schedule for the email campaign with a POST to /v2/emailmarketing/campaigns/CAMPAIGNID/schedules - http://developer.constantcontact.com/docs/campaign-scheduling/campaign-schedule-collection.html?meth...

You can also find both of these endpoints in the io-docs for testing. Please let me know if you have any questions!

 

Sincerely,

Elijah G.
API Support Engineer
Occasional Participant

Re: using ajax to access API

Hi Elijah G,

Thank you for your reply. I'm not sure about it, your links are used to create(POST)/get(GET) a campaign. I mean, i have a campaign and i want to send email to all member of a list, Please check attached file.

When i check all methods at https://constantcontact.mashery.com/io-docs, not sure but i don't see any method to do a same action like /emailmarketing/campaigns/campaignId/tests yet. That method should have a campaignID (i think)

Thanks and best regards,

Khoa Pham


constant-contact.png
Honored Contributor

Re: using ajax to access API

Hello,

 

If you have an existing campaign you wish to send, then you must do the following:

 

GET to campaigns collection: http://developer.constantcontact.com/docs/email-campaigns/email-campaigns-collection.html

 - This will allow you to get the campaign ID that is needed. You can search the results for the campaign by name.

GET and PUT to individual campaign: http://developer.constantcontact.com/docs/email-campaigns/email-campaign-resource.html?method=PUT

 - This step allows you to get the details and then assign the list(s) to which the campaign shall be sent. To do this you must inculde the list IDs in the "sent_to_contact_lists" property of the campaign object.

POST to email campaign schedules: http://developer.constantcontact.com/docs/campaign-scheduling/campaign-schedule-collection.html?meth...

 - This step is what triggers the email campaign to be sent. You specify a date and time, and the campaign will be sent at that to time the lists that are associated with the campaign.

 

These steps are fundamentally different from the test-send flow because our platorm is designed to send campaigns to assigned lists, rather than individual emails. Please let me know if you have any questions!

 

Sincerely,

Elijah G.
API Support Engineer
Still need help?
You can post a new message in the Community or find us on Twitter Mon-Fri 8am - 8pm ET. We've got real people waiting to help you out. Click below to start a conversation!