在回调中调用函数时出现意外的令牌错误
unexpected token error calling a function in a callback
在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);
})();
相关文章:
- React JS:未捕获(在承诺中)语法错误:在位置 0 的 JSON 中意外<令牌
- jQuery$.getJSON抛出意外令牌
- 为什么我在下面的..of循环中得到意外令牌
- 组件中的ReactJS意外令牌
- AngularJS $http GET 请求到本地 JSON 文件返回语法错误:意外令牌]
- 巴别塔,意外令牌 (15:33) ::.
- Sencha 2.0 和 Codeigniter RESTful API 生成未捕获的语法错误:意外令牌:
- 在事件上使用 jquery 时出现意外令牌
- JSON 中位置 4 处的意外令牌<
- 意外令牌 ) 在我的代码中
- Electron 和 Babel 6 异步/等待抛出意外令牌
- “JSON.parse(str)”返回“意外令牌c”,但str是一个字符串
- jQuery after() uncatch SyntaxError: 意外令牌 ILLEGAL
- 使用节点javascript的意外令牌ILLEGAL
- Javascript/JQuery-Chrome意外令牌
- Ajax 帖子未捕获语法错误:意外令牌非法
- angularjs-SyntaxError:请求从服务器返回时出现意外令牌
- jquery:使用ember的意外令牌C
- React本机项目中的意外令牌
- 解析有效的 JSON 得到“语法错误:意外令牌非法”