javascript 在回调中调用函数时出现意外令牌错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9247382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unexpected token error calling a function in a callback
提问by johowie
on the call back after the ajax call in qm150_submit $.post .... I want to call a second function called 'send_email' (which also has a callback called 'success_callback'
在 qm150_submit $.post 中的 ajax 调用之后的回调......我想调用第二个名为“send_email”的函数(它也有一个名为“success_callback”的回调
I am getting an error here
我在这里遇到错误
function () {send_email(fromName,fromEmail,toEmail,subject,message,success_callback) };
error: Uncaught SyntaxError: Unexpected token )
错误:未捕获的语法错误:意外的令牌)
here is the code :
这是代码:
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: and the code for the send_email()
编辑:以及 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;
}
回答by JimmiTh
The unexpected token is the (
after function
.
意外的标记是(
after function
。
First of all, you're declaring an anonymous function without ever calling it. Secondly, an anonymous function declaration cannot be a statement (or in other words, a function statement must have a name), which is why the (
is unexpected (javascript expects a function name, not parantheses).
首先,您声明了一个匿名函数而从未调用过它。其次,匿名函数声明不能是一个语句(或者换句话说,一个函数语句必须有一个名字),这就是为什么(
是意外的(javascript 需要一个函数名,而不是括号)。
Simply call send_email
directly... It's already inside a function, so it won't "pollute" the global object (there's nothing to pollute it with anyway) - I see no need for an anonymous function:
只需send_email
直接调用......它已经在一个函数内,所以它不会“污染”全局对象(无论如何都没有什么可以污染它) - 我认为不需要匿名函数:
alert('I am now calling the send_email for real!');
send_email(fromName, fromEmail, toEmail, subject, message, success_callback);
回答by gdoron is supporting Monica
If you check your code with JSLint you will get this error:
如果你用 JSLint 检查你的代码,你会得到这个错误:
Function statements cannot be placed in blocks. Use a function expression or move the statement to the top of the outer function.
函数语句不能放在块中。使用函数表达式或将语句移到外部函数的顶部。
You have to wrap the anonymous function like this: (function{})();
你必须像这样包装匿名函数: (function{})();
Fixed code:
固定代码:
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);
})();
}
});
}?
Or simply remove that anonymous function that does nothing:
或者干脆删除那个什么都不做的匿名函数:
send_email(fromName, fromEmail, toEmail, subject, message, success_callback);
instead of:
(function() {send_email(fromName, fromEmail, toEmail, subject, message, success_callback);
})();