2012-02-12 42 views
0

在qm150_submit $ .post中的ajax調用之後的回調中。 我想調用第二個名爲'send_email'的函數(它也有一個名爲'success_callback'的回調函數「意外的令牌錯誤調用回調中的函數

我正在這裏一個錯誤

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

錯誤:未捕獲的SyntaxError:意外的標記)

這裏是代碼:

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 
} 

編輯:和爲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; 
} 

回答

1

意外令牌function(

首先,你聲明一個匿名函數,而不用調用它。其次,匿名函數聲明不能​​是一個語句(或換句話說,函數聲明必須有一個名稱),這就是爲什麼(是意外(JavaScript期望函數名稱,而不是pamentalheses)。

只需直接調用send_email ...這已經是一個函數裏面,所以它不會「污染」的全局對象(沒有什麼用,污染也無妨) - 我認爲沒有必要爲一個匿名函數:

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

這是有道理的,現在我有一個引用問題,從該secondcallback獲取此錯誤'未捕獲的TypeError:對象[對象DOMWindow]的屬性'send_email'不是函數'我會開始一個新的問題,如果除非你或某人有一些快速的建議 – johowie 2012-02-12 08:49:26

+0

嗯,看起來你的函數實際上被稱爲'$ send_email',而不是'send_email'?如果沒有看到更多的代碼,肯定不會知道。 – JimmiTh 2012-02-12 09:00:02

+0

$ send_email是一個真或假的變量,如果$ send_email爲真,send_email()是一個應該被調用的函數。我將編輯我的帖子以包含send_email()函數。 – johowie 2012-02-12 09:09:19

0

代碼如果您使用的JSLint檢查你的代碼,您將收到此錯誤:

Function statements cannot be placed in blocks. Use a function expression or move the statement to the top of the outer function.

您必須包裝這樣的匿名函數:(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); 
     })(); 
+0

隨着這種改變功能被稱爲加載任何頁面上的腳本,而我只希望它被稱爲回調到$ .post()函數 – johowie 2012-02-12 08:35:59