Javascript and cross-domain problems

SOLVED
Go to solution
Frequent Participant

Javascript and cross-domain problems

Assuming I manage to authenticate and get the username, and have everything I need to start accessing the API. 


Is it possible to do access the API with javascript? Or would it be halted by the same origin policy / cross site call problem?

 

I've read JSONP can provide a workaround, but is it possible to use Ajax and JSONP with CC to successfully avoid all server-side programming? 

If JSONP doesent work(and it seems that it at best works only for GET requests), is there another way to solve it?
Or am I pretty much forced to use some server-side  programming?

1 ACCEPTED SOLUTION
Moderator

You are correct, there is no way to access a REST API through JavaScript.  The JSONP workaround is possible if the API in question is specifically designed to support JSONP, which the Constant Contact API is not.  We do provide wrapper libraries in PHP, C# and a few other open source wrappers.  It's definitely possible for you to implement those wrappers and return to yourself a JSON object, which you can then use via JavaScript requests in an AJAX style, even though the requests are being processed on your server behind the scenes.

Dave Berard
Senior Product Manager, Constant Contact

View solution in original post

7 REPLIES 7
Moderator

You are correct, there is no way to access a REST API through JavaScript.  The JSONP workaround is possible if the API in question is specifically designed to support JSONP, which the Constant Contact API is not.  We do provide wrapper libraries in PHP, C# and a few other open source wrappers.  It's definitely possible for you to implement those wrappers and return to yourself a JSON object, which you can then use via JavaScript requests in an AJAX style, even though the requests are being processed on your server behind the scenes.

Dave Berard
Senior Product Manager, Constant Contact

View solution in original post

I am stuck with the same issue..could you please help in solving the issue

@Minum:
I basically gave up.
I don't know if Constant Contact have modified their API since I tried this,

but basically, it is IMPOSSIBLE to send http-requests to foreign URLs from javascript, unless the sites you want to request(in thsi case Constant Contacts API), contain built in support for CORS and/or JSONP.

 

So, bottom line is, unless Constant Contact have modifeid their API to use CORS, you have two options: 1) Give up. 2) Use some server-side code like C#, PHP etc instead of client-side javascript.

At this time we have no plans to support JSONP but we are currently looking into the security and feasibility of CORS.  I don't have an ETA on that investigation but once our security team completes their assessment, I will update on if we are or are not going to be releasing support for CORS. 

Dave Berard
Senior Product Manager, Constant Contact

Any progress with CORS?

Hello,

 

Based on the feedback from our security team, there are some significant hurdles in the way for us in implementing this functionality for our APIs. Because of this, we are currently not setting any timelines for adding this, and continuing to listen for feedback from our developer community. 

 

Sincerely,

Elijah G.
API Support Engineer
Occasional Participant


corsmonster.jpeg
Developer Portal

View API documentation, code samples, get your API key.

Visit Page