javascript jQuery Ajax POST 不适用于 MailChimp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5188418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:18:42  来源:igfitidea点击:

jQuery Ajax POST not working with MailChimp

javascriptjqueryajaxjsonmailchimp

提问by jeffreynolte

I have the following code I am using to send data to a MailChimp Newsletter List (API v3). Everytime I remove the type: POSTfrom 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.

我有以下代码用于将数据发送到 MailChimp 通讯列表 (API v3)。每次我type: POST从函数中删除它时,它都会尝试通过 GET 发布数据并正确发送数据(MailChimp API 仪表板中的响应正常)。在浏览器 (FF) 中对此进行测试时,我得到一个带有“true”响应的 .part 文件。

   $(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

江南

回答by sid

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

有一个未公开的端点使用 JSONP 进行跨域 ajax 请求。

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.

只是改变“帖子?” 'post-json?' 并添加 '&c=?' 到标准 url 的末尾以获取 JSONP 端点。这不需要在客户端公开 API 密钥,也不需要创建服务器端视图。

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

我写了一个使用这种方法的 jQuery 插件,如果这有用的话

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

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

回答by jesse

The main issue is what jc commented on your original post - this simply won't work due to Same Origin Policy issues. Firebug is not as vocal about why the GET call fails, but that's why it returns no data. If you watch that with the POST, you'll see Firefox doesn't even make the call. Chrome's js console on the other hand straight explains the Same Origin policy to you.

主要问题是 jc 对您原始帖子的评论 - 由于同源策略问题,这根本不起作用。Firebug 并没有明确说明 GET 调用失败的原因,但这就是它不返回任何数据的原因。如果您使用 POST 观察它,您会看到 Firefox 甚至不拨打电话。另一方面,Chrome 的 js 控制台直接向您解释了同源策略。

All in all, this is a very good thing if for no other reason than it prevents you from publicly publishing your account's API Key, which is a very bad thing to do. If the reason why doesn't immediately sink in, go read through the large number of methods available in the API and then realize that all you need to access them is that API Key.

总而言之,这是一件非常好的事情,如果没有其他原因,它会阻止您公开发布您帐户的 API 密钥,这是一件非常糟糕的事情。如果没有立即理解的原因,请通读 API 中可用的大量方法,然后意识到访问它们所需的只是那个 API Key。

The correct way to do this is to POST data back to your server, then make the request from there. There are several fully built PHP examples (one using jquery, even), here.

执行此操作的正确方法是将数据 POST 回您的服务器,然后从那里发出请求。有几个完全建立PHP的例子(使用一个jQuery的,甚至),在这里

回答by Joseadrian

e.preventDefault();
data = {
  "apikey" : "667378947", 
  "id" : "90298590285", 
  "email_address" : "[email protected]", 
  "output" : "json"
};

Could be? Semicolon is important. Hehe

可能?分号很重要。呵呵