We are trying to communicate with a remote API which requires JSON formatted data. We tried to submit this using JQuery as follows, but were getting SOP errors:
<script type="text/javascript">
$.ajax({
username: 'username',
password: 'password',
url: "https://api.e2ma.net/123/members/add",
dataType: 'json',
type: 'POST',
data:
{
"fields": {
"name_first": "Name"
},
"email": "email@domain.com"
}
});
</script>
Unfortunately, their API does not support JSONP.
How can we post this JSON data to their remote API directly from within Coldfusion 9?
Also, what do we need to do to parse the response from their API?
Here is a link to the docs: http://myemma.com/api-docs/
And here is snippet of a simple add new member call from those docs:
import_single_member(account_id)
POST /#account_id/members/add
Adds or updates an audience member
Parameters:
email (string) – Email address of member to add or update
fields (dictionary) – Names and values of user-defined fields to update
group_ids (integer) – Optional. Add imported members to this list of groups.
signup_form_id – Optional. Indicate that this member used a particular signup form.
Returns:
The member_id of the new or updated member, and whether the member was added or an existing member was updated
Example:
POST /123/members/add
{
"fields": {
"first_name": "Benjamin"
},
"email": "benjamin@myemma.com"
}
{
"added": true,
"member_id": 1024
}
Thanks.
You’ll need to build a proxy since you can’t POST to another server from JavaScript. Your JS code should look something like:
And your ColdFusion code should look something like this:
I’m not sure if the fields will be in the format needed but ti should be pretty close.
You can also return whatever you get back to JavaScript like this after you do the post: