在回调中调用函数时出现意外的令牌错误

unexpected token error calling a function in a callback

本文关键字:意外 令牌 错误 回调 调用 函数      更新时间:2023-09-26

在qm150_submit$.post中ajax调用后的回调。。。。我想调用第二个名为"send_email"的函数(它也有一个回调名为"success_callback"

我在这里得到一个错误

function () {send_email(fromName,fromEmail,toEmail,subject,message,success_callback) };

错误:未捕获语法错误:意外的令牌)

这是代码:

function qm150_submit($title, $name, $email, $description, $send_email) {
  $.post('<?PHP print API_SUBMIT; ?>', { "title": $title, "name": $name, "email": $email, "description": $description },
    function (data) {          // callback function after API_SUBMIT
    // Send email with a link to their collection
      if ($send_email) {
        // parameters for the send_email() ajax function
        var subject = "subject";
        var collection_id = data.collection_id;  // data is json returned from the ajax above
        var toEmail = $email
        var message = "<?PHP print SHARE_COLLECTION;?>"+collection_id;
        var fromEmail = "<?PHP print EMAIL_FROM_EMAIL; ?>";
        var fromName = "<?PHP print EMAIL_FROM_NAME; ?>";
        var success_callback = function (results) { 
          alert('send_email has returned with: '+results);
        };
        alert('I am now calling the send_email');
        function () {send_email(fromName,fromEmail,toEmail,subject,message,success_callback) };
      }
    });
        // missing a curly bracket ? no! note  double indentation of the anonymous function (data) is a continuation of first statement
}

edit:以及send_email()的代码

function send_email(fromName,fromEmail,toEmail,subject,message,success_callback) {
  alert('send_email called');
  $.ajax({
    type: 'post',
    url: '<?PHP print API_SHARE_EMAIL;?>',
    data: 'fromName=' + fromName + '&fromEmail=' + fromEmail + '&toEmail=' + toEmail + '&subject=' + subject + '&message=' + message,
    dataType:'json',
    success: success_callback
  });
  alert('send_email finished');
  return true;
}

意外的令牌是function之后的(

首先,您声明的是一个匿名函数,而不需要调用它。其次,匿名函数声明不能是一个语句(或者换句话说,函数语句必须有一个名称),这就是为什么(是意外的(javascript需要一个函数名,而不是paranthes)。

只需直接呼叫send_email。。。它已经在一个函数中了,所以它不会"污染"全局对象(无论如何都没有什么可污染的)-我认为没有必要使用匿名函数:

alert('I am now calling the send_email for real!');
send_email(fromName, fromEmail, toEmail, subject, message, success_callback);

如果你用JSLint检查你的代码,你会得到这个错误:

函数语句不能放在块中。使用函数表达式或将语句移到外部函数的顶部。

您必须像这样包装匿名函数:(function{})();

固定代码:

function qm150_submit($title, $name, $email, $description, $send_email) {
$.post('<?PHP print API_SUBMIT; ?>', {
    "title": $title,
    "name": $name,
    "email": $email,
    "description": $description
}, function(data) { // callback function after API_SUBMIT
    // Send email with a link to their collection
    if ($send_email) {
        // parameters for the send_email() ajax function
        var subject = "subject";
        var collection_id = data.collection_id; // data is json returned from the ajax above
        var toEmail = $email;
        var message = "<?PHP print SHARE_COLLECTION;?>" + collection_id;
        var fromEmail = "<?PHP print EMAIL_FROM_EMAIL; ?>";
        var fromName = "<?PHP print EMAIL_FROM_NAME; ?>";
        var success_callback = function(results) {
            alert('send_email has returned with: ' + results);
        };
        alert('I am now calling the send_email');
        (function() {
            send_email(fromName, fromEmail, toEmail, subject, message, success_callback);
        })();
    }
});
}​

或者简单地删除什么都不做的匿名函数:

send_email(fromName, fromEmail, toEmail, subject, message, success_callback);
instead of:
(function() {send_email(fromName, fromEmail, toEmail, subject, message, success_callback);
        })();