javascript 语法错误:预期的表达式,得到 ')'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29812338/
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
SyntaxError: expected expression, got ')'
提问by plasticbullet
I've been stuck at this error for a few days and still couldn't figure out what is wrong. Would be great if someone could just point me to the right direction of solving this issue.
我已经被这个错误困住了几天,但仍然无法弄清楚出了什么问题。如果有人能指出我解决这个问题的正确方向,那就太好了。
Update:I realise that error is gone when I commented "addMessages(xml)" in the updateMsg() function. How do I make it work then?
更新:当我在 updateMsg() 函数中注释“addMessages(xml)”时,我意识到错误消失了。那我该如何让它工作呢?
Error:http://i.imgur.com/91HGTpl.png
错误:http : //i.imgur.com/91HGTpl.png
Code:
代码:
$(document).ready(function () {
var msg = $("#msg");
var log = $("#log");
var timestamp = 0;
$("#name").focus();
$("#login").click(function() {
var name = $("#name").val();
if (!name) {
alert("Please enter a name!");
return false;
}
var username = new RegExp('^[0-9a-zA-Z]+$');
if (!username.test(name)){
alert("Invalid user name! \n Please do not use the following characters \n `~!@#$^&*()=|{}':;',\[\].<>/?~@#");
return false;
}
$.ajax({
url: 'login.php',
type: 'POST',
dataType: 'json',
data: {name: name},
success: function() {
$(".login").hide();
}
})
return false;
});
$("#form").submit(function() {
if (!msg.val()) {
return false;
}
$.ajax({
url: 'add_message.php',
type: 'POST',
dataType: 'json',
data: {message: msg.val()},
})
msg.val("");
return false
});
window.setInterval(function () {
updateMsg();
}, 300);
function updateMsg() {
$.post('server.php', {datasize: '1024'}, function(xml) {
addMessages(xml);
});
}
function addMessages(xml) {
var json = eval('('+xml+')');
$.each(json, function(i, v) {
tt = parseInt(v.time);
if (tt > timestamp) {
console.log(v.message);
appendLog($("<div/>").text('[' + v.username + ']' + v.message));
timestamp = tt
}
});
}
function appendLog(msg) {
var d = log[0]
var doScroll = d.scrollTop == d.scrollHeight - d.clientHeight;
msg.appendTo(log)
if (doScroll) {
d.scrollTop = d.scrollHeight - d.clientHeight;
}
}
});
回答by redbmk
It might help to read up on evala bit. It looks like it doesn't do what you think it does.
稍微了解一下eval可能会有所帮助。看起来它不像你认为的那样做。
eval() is a dangerous function, which executes the code it's passed with the privileges of the caller.
eval() 是一个危险的函数,它以调用者的权限执行它传递的代码。
Also
还
There are safer (and faster!) alternatives to eval() for common use-cases.
对于常见用例,有更安全(更快!)的 eval() 替代方法。
It looks like what you're trying to do is get data from the server in the form of JSON. You'll need to make sure that your server returns something that is valid JSON, which you can verify here. Most server-side programming languages have a library that will turn an object into JSON to make that a piece of cake. Here's an example for php.
看起来您要做的是以 JSON 的形式从服务器获取数据。您需要确保您的服务器返回有效 JSON 的内容,您可以在此处进行验证。大多数服务器端编程语言都有一个库,可以将对象转换为 JSON,使之成为小菜一碟。这是 php 的示例。
On the client-side, you'll need to change var json = eval('(' + xml + ')');
to var json = JSON.parse(xml);
This will give you the javascript version of your php/perl/python/etc object. If it's an array, you can then iterate through it with a for
loop, Array.prototype.forEach
, or a variety of functions from different libraries, such as $.each
or _.each
.
在客户端,您需要更改var json = eval('(' + xml + ')');
为var json = JSON.parse(xml);
This will give you the javascript version of your php/perl/python/etc object. 如果它是一个数组,则可以使用for
循环Array.prototype.forEach
、 或来自不同库的各种函数(例如$.each
或 )对其进行迭代_.each
。
回答by Katrin
SyntaxError: expected expression, got ')' usually cause by something like
SyntaxError: 预期的表达式,得到 ')' 通常是由类似的原因引起的
exeFunction(a,b,)
See if your form submit function ajax causing such error
查看您的表单提交功能ajax是否导致此类错误
$("#form").submit(function() {
if (!msg.val()) {
return false;
}
$.ajax({
url: 'add_message.php',
type: 'POST',
dataType: 'json',
data: {message: msg.val()}, <-------
})
msg.val("");
return false
});
回答by Anoop P S
If you are triggering the java script on click or trigger any click. sometimes missing of 0 gives the above error.
如果您在单击时触发 java 脚本或触发任何单击。有时缺少 0 会导致上述错误。
<a href="javascript:void(0);">delete</a>
回答by Steve
would JSON.stringify({datasize: '1024'}) do the trick? just a guess
JSON.stringify({datasize: '1024'}) 会起作用吗?只是一个猜测