[Proto-Scripty] Re: problem with myajax oncomplete callback function
Hi all, here is my latest code, still doesnt work with ie, but it is cleaner and neater now, before i continue, i would like to thanks those who have try to help me.. =D A variable call "last" is use to track the latest post id, so that i could use it to query the database and retrieve messages after the last retrieve one. Ajax caller - function getmessage() { var pars = 'last=' + last; var myAjaxget = new Ajax.Request('json.php', { method: 'get', parameters: pars, onComplete: loadcomplete }); window.setTimeout("getmessage()", 2000); } Callback --- function loadcomplete(request) { var arrlast = []; var elDisplay = $('display'); if (!elDisplay) return; var postTemplate = new Template( ''+ '#{date}'+ '#{name}:'+ ' #{content}' ); console.log(request.responseJSON.message); //var response = request.responseJSON.evalJSON(); var response = request.responseJSON; if (!response || (response && !response.message)) return; $A(response.message).each(function(m) { elDisplay.insert({ top: postTemplate.evaluate(m) }) arrlast.push(m.id); }) last = arrlast.max(); }; response from Json.php - {"message":[{"id":"300","name":"name","content":"d","date":"Mon, 29 Sep, 03:51 PM"}]} --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: problem with myajax oncomplete callback function
Hi Matt, I have remove the trailing comma from the Json already, but it is still the same.. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: problem with myajax oncomplete callback function
There is a trailing comma in your object that IE doesn't like, considering a syntax error. Remember when evaling something that the expression you're evaluating has to have perfect syntax as well. On Sep 28, 12:39 pm, uncleroxk <[EMAIL PROTECTED]> wrote: > ok my JSON: > > {"messages": {"message":[ {"id": "75", > > "user": "name", > >"text": "message", > > "time": "Sun, 28 Sep, 08:19 > PM" > > },]}} > > -- > i have tried this, and there are data coming in..., but the alert show > up as "undefined" in firefox, and no popup in ie 7 > > var myAjaxget = new Ajax.Request('json.php', > { > method: 'get', > parameters: pars, > onComplete: function(request) > { > alert(request.responseJSON.messages.message.user);} > > }); > window.setTimeout("getmessage()", 2000); > > } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: problem with myajax oncomplete callback function
On Sep 29, 12:22 am, kangax <[EMAIL PROTECTED]> wrote: > On Sep 28, 11:10 am, uncleroxk <[EMAIL PROTECTED]> wrote: > > > > > I have try to change it to "var response = eval(request.responseJSON); > > ".. and add "header("Content-type: application/json");" on my php page > > that generate the Json.. but it is still not working... > > > here is my updated version.. > > > -- > > //callback function > > function loadcomplete(request) > > { > > var arrlast=new Array(); > > var response = eval(request.responseJSON); //updated > > for(i=0; i< response.messages.message.length; i++) { > > var post = ' > id="'+response.messages.message[i].id+'"> > class="date">'+response.messages.message[i].time+' > class="username">'+response.messages.message[i].user+': > span>'+response.messages.message[i].text+''; > > $('display').innerHTML = post + $ > > ('display').innerHTML; > > } > > > } > > You could easily simplify this snippet: > > function loadcomplete(request) { > var arrlast = [], > post, > current, > elDisplay = $('display'); > > if (!elDisplay) return; > > var postTemplate = new Template( > ''+ > '#{time}'+ > '#{user}:'+ > ' #{text}' > ); > > var response = request.responseJSON.evalJSON(); > if (!response || (response && !response.messages)) return; > > $A(response.messages.message).each(function(m) { > elDisplay.insert({ > top: postTemplate.evaluate(m) > }) > }) > > }; > > > //an example of my Json > > {"messages": {"message":[ {"id": "75", > > > "user": "name", > > > "text": "message", > > > "time": "Sun, 28 Sep, 08:19 > > PM" > > > },]}} > > ---^ > A trailing comma. Need to remove it. > > [snip code] > > -- > kangax hi kangax, i cannot get this to work.. can you explain more on this part: var arrlast = [], post, current, elDisplay = $('display'); thanks --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: problem with myajax oncomplete callback function
ok my JSON: {"messages": {"message":[ {"id": "75", "user": "name", "text": "message", "time": "Sun, 28 Sep, 08:19 PM" },]}} -- i have tried this, and there are data coming in..., but the alert show up as "undefined" in firefox, and no popup in ie 7 var myAjaxget = new Ajax.Request('json.php', { method: 'get', parameters: pars, onComplete: function(request) { alert(request.responseJSON.messages.message.user); } }); window.setTimeout("getmessage()", 2000); } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: problem with myajax oncomplete callback function
On Sep 28, 11:10 am, uncleroxk <[EMAIL PROTECTED]> wrote: > I have try to change it to "var response = eval(request.responseJSON); > ".. and add "header("Content-type: application/json");" on my php page > that generate the Json.. but it is still not working... > > here is my updated version.. > > -- > //callback function > function loadcomplete(request) > { > var arrlast=new Array(); > var response = eval(request.responseJSON); //updated > for(i=0; i< response.messages.message.length; i++) { > var post = ' id="'+response.messages.message[i].id+'"> class="date">'+response.messages.message[i].time+' class="username">'+response.messages.message[i].user+': span>'+response.messages.message[i].text+''; > $('display').innerHTML = post + $ > ('display').innerHTML; > } > > } You could easily simplify this snippet: function loadcomplete(request) { var arrlast = [], post, current, elDisplay = $('display'); if (!elDisplay) return; var postTemplate = new Template( ''+ '#{time}'+ '#{user}:'+ ' #{text}' ); var response = request.responseJSON.evalJSON(); if (!response || (response && !response.messages)) return; $A(response.messages.message).each(function(m) { elDisplay.insert({ top: postTemplate.evaluate(m) }) }) }; > > //an example of my Json > {"messages": {"message":[ {"id": "75", > > "user": "name", > > "text": "message", > > "time": "Sun, 28 Sep, 08:19 > PM" > > },]}} ---^ A trailing comma. Need to remove it. [snip code] -- kangax --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: problem with myajax oncomplete callback function
Hi, You don't have to eval the responseJSON. The point of the responseJSON property is that the JSON has already been decoded into an object for you. So if you have page returning: { "foo": "bar" } and your handler looks like this function oncomplete(response) { alert(response.responseJSON.foo); } ...then provided no errors occur you'll see an alert showing "bar". -- T.J. Crowder tj / crowder software / com On Sep 28, 4:10 pm, uncleroxk <[EMAIL PROTECTED]> wrote: > I have try to change it to "var response = eval(request.responseJSON); > ".. and add "header("Content-type: application/json");" on my php page > that generate the Json.. but it is still not working... > > here is my updated version.. > > -- > //callback function > function loadcomplete(request) > { > var arrlast=new Array(); > var response = eval(request.responseJSON); //updated > for(i=0; i< response.messages.message.length; i++) { > var post = ' id="'+response.messages.message[i].id+'"> class="date">'+response.messages.message[i].time+' class="username">'+response.messages.message[i].user+': span>'+response.messages.message[i].text+''; > $('display').innerHTML = post + $ > ('display').innerHTML; > } > > } > > //an example of my Json > {"messages": {"message":[ {"id": "75", > > "user": "name", > > "text": "message", > > "time": "Sun, 28 Sep, 08:19 > PM" > > },]}} > > > > One more thing.. > > function loadcomplete(request) > { > > alert(request.responseJSON); //work both in ie and firefox, both alert > show as object > > alert(request.responseJSON.messages.message.length); //work with > firefox but nothing show up on ie > > var arrlast=new Array(); > var response = eval(request.responseJSON); //updated > for(i=0; i< response.messages.message.length; i++) { > var post = ' id="'+response.messages.message[i].id+'"> class="date">'+response.messages.message[i].time+' class="username">'+response.messages.message[i].user+': span>'+response.messages.message[i].text+''; > $('display').innerHTML = post + $ > ('display').innerHTML; > } > > } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: problem with myajax oncomplete callback function
I have try to change it to "var response = eval(request.responseJSON); ".. and add "header("Content-type: application/json");" on my php page that generate the Json.. but it is still not working... here is my updated version.. -- //callback function function loadcomplete(request) { var arrlast=new Array(); var response = eval(request.responseJSON); //updated for(i=0; i< response.messages.message.length; i++) { var post = ''+response.messages.message[i].time+''+response.messages.message[i].user+': '+response.messages.message[i].text+''; $('display').innerHTML = post + $ ('display').innerHTML; } } //an example of my Json {"messages": {"message":[ {"id": "75", "user": "name", "text": "message", "time": "Sun, 28 Sep, 08:19 PM" },]}} One more thing.. function loadcomplete(request) { alert(request.responseJSON); //work both in ie and firefox, both alert show as object alert(request.responseJSON.messages.message.length); //work with firefox but nothing show up on ie var arrlast=new Array(); var response = eval(request.responseJSON); //updated for(i=0; i< response.messages.message.length; i++) { var post = ''+response.messages.message[i].time+''+response.messages.message[i].user+': '+response.messages.message[i].text+''; $('display').innerHTML = post + $ ('display').innerHTML; } } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: problem with myajax oncomplete callback function
On Sep 28, 10:00 am, Blaze <[EMAIL PROTECTED]> wrote: > Hello, > > indeed something wrong here: > var response = eval("(" + request.responseText + ")"); > > You should just use: > var response = eval(request.responseText); Not really. Those parenthesis around a to-be-evaluated string make it (syntactically) an expression statement (rather than a block statement). Making it an expression allows to evaluate things like `'{foo: "bar"}'` (object literals) properly. > > Also I hope that you have right response for that 'for loop' to work. > > Cheers -- kangax --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: problem with myajax oncomplete callback function
Hello, indeed something wrong here: var response = eval("(" + request.responseText + ")"); You should just use: var response = eval(request.responseText); Also I hope that you have right response for that 'for loop' to work. Cheers On Sep 28, 2:21 pm, uncleroxk <[EMAIL PROTECTED]> wrote: > Hi, i have a problem with this ajax oncomplete callback function, it > works with firefox but not in internet explorer.. i tried to debug > with firebug, everythings seems ok until this line of code.. > > var response = eval("(" + request.responseText + ")"); //i suspect > something wrong in here > > //callback function > function loadcomplete(request) > { > var arrlast=new Array(); > var response = eval("(" + request.responseText + ")"); //i suspect > something wrong here.. > for(i=0; i< response.messages.message.length; i++) { > var post = ' id="'+response.messages.message[i].id+'"> class="date">'+response.messages.message[i].time+' class="username">'+response.messages.message[i].user+': span>'+response.messages.message[i].text+''; > $('display').innerHTML = post + > $('display').innerHTML; > } > > } > > //an example of my Json > {"messages": {"message":[ {"id": "75", > > "user": "name", > > "text": "message", > > "time": "Sun, 28 Sep, 08:19 PM" > > },]}} > > thanks.. =) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: problem with myajax oncomplete callback function
Hi, Why not use the responseJSON property? http://www.prototypejs.org/api/ajax/response Also: > //callback function > function loadcomplete(request) Your variable name there is probably misleading, as the callback receives an Ajax.Response object, not the Ajax.Request object. HTH, -- T.J. Crowder tj / crowder software / com On Sep 28, 1:21 pm, uncleroxk <[EMAIL PROTECTED]> wrote: > Hi, i have a problem with this ajax oncomplete callback function, it > works with firefox but not in internet explorer.. i tried to debug > with firebug, everythings seems ok until this line of code.. > > var response = eval("(" + request.responseText + ")"); //i suspect > something wrong in here > > //callback function > function loadcomplete(request) > { > var arrlast=new Array(); > var response = eval("(" + request.responseText + ")"); //i suspect > something wrong here.. > for(i=0; i< response.messages.message.length; i++) { > var post = ' id="'+response.messages.message[i].id+'"> class="date">'+response.messages.message[i].time+' class="username">'+response.messages.message[i].user+': span>'+response.messages.message[i].text+''; > $('display').innerHTML = post + > $('display').innerHTML; > } > > } > > //an example of my Json > {"messages": {"message":[ {"id": "75", > > "user": "name", > > "text": "message", > > "time": "Sun, 28 Sep, 08:19 PM" > > },]}} > > thanks.. =) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: problem with myajax oncomplete callback function
Edit.. i mean i debug with the Alert function in IE... with firebug on firefox is fine.. Alert shows up well in firefox too, but not in IE --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---