POST函数运行两次,第一次API调用失败,第二次运行。

时间:2017-01-05 23:11:51

标签: javascript ajax node.js mailchimp

我有一个订阅对话框,它接收电子邮件地址,然后将其插入Mailchimp。

它抛出的错误使得体验不是很好。经过进一步调查,看起来POST正在运行两次。

第一次运行api调用总是失败,但随后又运行并运行,电子邮件将放入数据库中。

我试图找到问题,因为它运行两次AJAX函数总是会触发错误,这意味着我无法根据函数成功做事(下面是变量subscribeissuccess的要点)。

非常感谢任何帮助。

ajax功能:

 $.ajax({
        type: "POST",
        url: "/subscribe",
        data: data, 
        success: function(data){
            $("#subscribe-form2 :input").prop("disabled", false);
            if(data.success){

                subscribeissuccess = 'TRUE';

            }   else {
                $('#subscribe-message2').html('Error occurred during subscribe. Please try again later.');
            }
        }, error: function(){
            $("#subscribe-form2 :input").prop("disabled", false);
            $('#subscribe-message2').html('Error occurred during during subscribe. Please try again later.');
        }
});

API插入/ subscribe:

module.exports = function(req, res){
var emailId = req.body.email;
var button = req.body.subscribe;
var api = require('../api');

var apikey = "removed";
var listid = "removed";

var body = JSON.stringify({apikey: apikey, id: listid, email: {'email': emailId}, merge_vars:{groupings:[{name:"MERGE1", groups:[button]}]}, double_optin: false, send_welcome: false}),
link = "/2.0/lists/subscribe.json";

api.call(link, body, function(data){
    try{
        var ret = JSON.parse(data);
        console.log(data);
        if(ret.leid && ret.euid) res.json({success: true});
        else if(ret.code && ret.code == 214) res.json({success: true});
        else res.json({success: false});
    } catch(e){
        res.json({success: false});
    }       
}, function(err){
    res.json({success: false});
});
}; 

' ... api'

的功能
module.exports = {
call: function (endpoint, body, callback, errcallback){
    var http = require('https');
    var options = {
        host: 'us5.api.mailchimp.com',
        post: 443,
        path: endpoint,
        headers: {
            "Content-Type": "application/json",
            "Content-Length": Buffer.byteLength(body),
            accept: '*/*'
        },
        method: 'POST'};

    var req = http.request(options, function(res){
        console.log('STATUS:' + res.statusCode);
        console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
        var data = '';
        res.on('data', function(chunk){
            data = data + chunk;
        });
        res.on('end', function(){
            callback(data);
        });
    });

    req.on('error', function(e){
        console.log('problem with request: ' + e.message);
        errcallback(e);
    });
    req.write(body);
    req.end();
}
};

成功添加电子邮件时收到的错误:

STATUS:500
HEADERS: {"server":"openresty","content-type":"application/json; charset=utf-8","content-length":"128","x-mailchimp-api-error-code":"-100","date":"Thu, 05 Jan 2017 22:59:51 GMT","connection":"close","set-cookie":["_AVESTA_ENVIRONMENT=prod; path=/"]}
{"status":"error","code":-100,"name":"ValidationError","error":"The email parameter should include an email, euid, or leid key"}
POST /subscribe 200 146ms - 17b
STATUS:200
HEADERS: {"server":"openresty","content-type":"application/json; charset=utf-8","content-length":"63","vary":"Accept-Encoding","date":"Thu, 05 Jan 2017 22:59:51 GMT","connection":"close","set-cookie":["_AVESTA_ENVIRONMENT=prod; path=/"]}
{"email":"ef@gmail.com","euid":"24d1d1b89e","leid":"118400873"}
POST /subscribe 200 277ms - 16b

1 个答案:

答案 0 :(得分:0)

当根据@barmar和@Xeren Narcy的建议采取行动时,我发现我有两个事件处理程序,每个事件处理程序都运行subscribe函数。一个在HTML表单中,一个在jquery.main.js中,当我删除一个问题就消失了。