jQuery Ajax POST not working with MailChimp

jeffreynolte picture jeffreynolte · Mar 4, 2011 · Viewed 18.5k times · Source

I have the following code I am using to send data to a MailChimp Newsletter List (API v3). Everytime I remove the type: POST from the function it attempts to post the data via GET and it sends the data properly (ok response in MailChimp API dashboard). When testing this in the browser (FF) I get a .part file with "true" response.

   $(function(){
     $("a#test").click(function(e){
       e.preventDefault()  
       data = {
         "apikey" : "667378947", 
         "id" : "90298590285", 
         "email_address" : "[email protected]", 
         "output" : "json"
       }

  $.ajax({ 
    type: "POST",
    url: 'http://us2.api.mailchimp.com/1.3/?method=listSubscribe',
    data: data,
    success: function(data){
      alert(data);
    },
    error: function(){
      alert("err");
    }
  })       
 });
});   

Im pulling my hair out on this one, any insight is greatly appreciated.

Thanks in advance,

JN

Answer

sid picture sid · May 4, 2013

There is an undocumented endpoint that uses JSONP to do cross-domain ajax requests.

Just change 'post?' to 'post-json?' and add '&c=?' to the end of the standard url to get the JSONP endpoint. This doesn't requires the API key to be exposed on the client-side, or the creation of a server-side view.

I wrote a jQuery plugin that uses this method, if that's useful at all

https://github.com/scdoshi/jquery-ajaxchimp