Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  173] [ 4]  / answers: 1 / hits: 18597  / 9 Years ago, thu, april 23, 2015, 12:00:00

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?



Error:
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;
}
}
});

More From » jquery

 Answers
14

It might help to read up on eval a bit. It looks like it doesn't do what you think it does.




eval() is a dangerous function, which executes the code it's passed with the privileges of the caller.




Also




There are safer (and faster!) alternatives to eval() for common use-cases.




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.



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.


[#66944] Tuesday, April 21, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
benitoh

Total Points: 150
Total Questions: 113
Total Answers: 104

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
benitoh questions
Sun, Mar 21, 21, 00:00, 3 Years ago
Mon, May 13, 19, 00:00, 5 Years ago
;