jQuery Ajax POST 无法与 MailChimp 一起使用
我有以下代码,用于将数据发送到 MailChimp 时事通讯列表 (API v3)。每次我从函数中删除 type: POST
时,它都会尝试通过 GET 发布数据并正确发送数据(MailChimp API 仪表板中的 ok 响应)。当在浏览器(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");
}
})
});
});
我对这个问题非常着迷,任何见解都将不胜感激。
预先感谢,
JN
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个未记录的端点使用 JSONP 进行跨域 ajax 请求。
只需更改“帖子”即可?到“post-json?”并添加“&c=?”到标准 url 的末尾以获取 JSONP 端点。这不需要在客户端公开 API 密钥,也不需要创建服务器端视图。
我编写了一个使用此方法的 jQuery 插件,如果它有用的话
https://github.com/scdoshi/ jquery-ajaxchimp
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
主要问题是 jc 对您原始帖子的评论 - 由于同源策略问题,这根本行不通。 Firebug 并没有明确说明 GET 调用失败的原因,但这就是它不返回任何数据的原因。如果您通过 POST 查看该内容,您会发现 Firefox 甚至没有进行调用。另一方面,Chrome 的 js 控制台直接向您解释同源策略。
总而言之,这是一件非常好的事情,如果没有其他原因,它会阻止您公开发布您帐户的 API 密钥,这是一件非常糟糕的事情。如果没有立即明白原因,请仔细阅读 API 中可用的大量方法,然后意识到访问它们所需的只是 API 密钥。
正确的方法是将数据 POST 回您的服务器,然后从那里发出请求。 此处有几个完全构建的 PHP 示例(甚至有一个使用 jquery)。
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.
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.
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.
可能是吗?分号很重要。呵呵
Could be? Semicolon is important. Hehe