Mailchimp如何在JavaScript中调用mailchimp 3.0 API [英] Mailchimp how to call mailchimp 3.0 API in javascript

查看:68
本文介绍了Mailchimp如何在JavaScript中调用mailchimp 3.0 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将电子邮件订阅到mailchimp上的列表,我首先遵循文档,使用邮递员"进行了请求,添加了所需内容,并且一切正常,因此我尝试在我的网站上进行操作没用

I am trying to subscribe an email to a list on mailchimp, I followed the documentation first, made a request using "Postman" added what was needed and everything works just fine, so I tried to do it on my website and it didn't work

我试图用与邮递员相同的值发出一个简单的请求,但是每次我尝试发送该请求时,响应都显示为

I tried to made a simple request with the same values I set on postman, but everytime I try to send the request the response says

XMLHttpRequest无法加载 https://us12.api.mailchimp.com/3.0/lists/xxxxxx/成员.回复 进行预检请求未通过访问控制检查:否 请求中存在"Access-Control-Allow-Origin"标头 资源.因此,不允许使用来源" https://mywebsite.com ". 使用权.响应的HTTP状态代码为501.

XMLHttpRequest cannot load https://us12.api.mailchimp.com/3.0/lists/xxxxxx/members. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://mywebsite.com' is therefore not allowed access. The response had HTTP status code 501.

我试图找到一种方法来克服这个问题,但这是不可能的

I tried to find a way to overcome this but it has been impossible

我在stackoverflow上进行搜索,每个人都说要使用jsonp或向ajax调用中添加某些内容,或者使用mailchimp ajax插件无效

I searched on stackoverflow everybody says to use jsonp or add something to the ajax call or use a mailchimp ajax plugin nothing has worked

我尝试了类似这样的不同stackoverflow帖子 Mailchimp使用jQuery AJAX进行订阅吗? 但几乎所有人都说同样的话

I tried diferent stackoverflow posts like this one Mailchimp subscribe using jQuery AJAX? but almost all of them say the same

我尝试了cache: false dataType:jsonp crossDomain: true xhrFields: {withCredentials: true}

这是我的代码,我正在使用Jquery

Here it is my code, I am using Jquery

$.ajax({
          type: "POST",
          url: "https://usxx.api.mailchimp.com/3.0/lists/xxxxxxxx/members",

          data: { "email_address":email@adress.com,  "status":"subscribed"},
          headers: {
            "Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==",
            "Content-Type": "application/json"
          },
          success: function(data){
             alert('Thanks for subscribing');
          },
          error: function(data){
            alert('there was an error, try again later');
          }
});

我还考虑过创建自己的api,然后调用mailchimp api,但我可能会遇到相同的问题

I also Thought on creating my own api and then make the call to mailchimp api but I might ran into the same problem

您有什么建议吗?

预先感谢

推荐答案

正如charliefl所指出的,这是一个CORS问题. MailChimp不支持CORS,主要是因为它需要您将API凭据传递给网页用户,以允许他们接管您的整个帐户.

As charliefl noted, this is a CORS issue. MailChimp doesn't support CORS, mostly because it would require you passing your API credentials to the user of the webpage, allowing them to takeover your entire account.

MailChimp的两个选项是通过服务器代理您的请求,或者,为了将人们注册到您的列表中,您可以

Your two options for MailChimp are to proxy your requests through a server or, for signing people up to your list, you can build a custom signup form that uses a much more restricted API. The caveat of this second method is that it forces all of your subscribes through MailChimp's double opt-in process.

这篇关于Mailchimp如何在JavaScript中调用mailchimp 3.0 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆