Jump to content
Sign in to follow this  
dcole.ath.cx

Displaying imported message

Recommended Posts

I was trying to import XML but it wasn't working and now I'm using JSON... Below is the code that isn't working. What are the errors in the code? The JSON is printing a variable called timestamp and an array called messages.

var lastPing = 0;function updateMsgOnBrowser(testXML) {	eval("var retval = " + xmlHttp.responseText);	var timestamp = retval.timestamp;	if (timestamp > lastPing) {			lastPing = timestamp;		var msg_display = document.getElementById("msg_display");				for (var i = 0; i < retval.messages.length; i++)		{			var tempdata = tempdata + "<br />" + retval.messages[i];		}		msg_display.innerHTML = tempdata;	}}

Share this post


Link to post
Share on other sites

If xmlHttp.responseText is defined then the error must be in your JSON - can you post the JSON-generating code?

Share this post


Link to post
Share on other sites

maybe xmlHttp.responsetext isn't defined, but then what would it be?... Here is everything elseThe PHP code below writes the JSON XML and encodes it.

$retval = array('timestamp' => $ts, 'messages' => $tempmessages);include "json.php";$json = new Services_JSON();echo $json->encode($retval);

And here is the XML out put, although to get this the PHP file couldn't get the POST data...

XML Parsing Error: not well-formedLocation: .../server.phpLine Number 1, Column 1:{"timestamp":1201453071,"messages":["Bot: My responses are limited.","User: ","Bot: hey","User: hi","Bot: hi","User: hi","Bot: hi","User: hi","Bot: hey","User: hi"]}^

That ^ above goes to the {, BTW

Share this post


Link to post
Share on other sites

Well, since JSON is not XML obviously the XML parser would not be able to parse it. The JSON itself, however, is well-formed. Try running alert(xmlHttp.resonseText) somewhere in the updateMsgOnBrowser() function to check whether it is defined.

Share this post


Link to post
Share on other sites

It would also be better to restructure the for loop to declare the variable first before you use it.

		var tempdata = "";		for (var i = 0; i < retval.messages.length; i++)		{			tempdata += "<br />" + retval.messages[i];		}

Make sure that the page sending the data is sending the correct mime type, it should just be using text/plain. If you're saying it's XML that will be an issue.

Share this post


Link to post
Share on other sites

So alert("Hello"); does work, but alert(xmlHttp.resonseText); does not. The server.php file is also a plain text file now.The code that calls updateMsgOnBrowser is the following code and it uses XML, should I change it or is there another way of getting the text test from server.php without xmlHttp.responseText?

function talktoServer(){	var req = newXMLHttpRequest();	//register the callback handler function	  var callbackHandler = getReadyStateHandler(req, updateMsgOnBrowser);	  req.onreadystatechange = callbackHandler;	  req.open("POST", "server.php", true);	  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	  //get the value from the text input element and send it to server	  var testmsg = document.getElementById("testmsg");	  var msg_value = testmsg.value;	  req.send("msg="+msg_value);}

Share this post


Link to post
Share on other sites

Why not just

var req;function talktoServer(){	req = new XMLHttpRequest();	//register the callback handler function	  req.onreadystatechange = updateMsgOnBrowser;	  req.open("POST", "server.php", true);	  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	  //get the value from the text input element and send it to server	  var testmsg = document.getElementById("testmsg");	  var msg_value = testmsg.value;	  req.send("msg="+msg_value);}

and

var lastPing = 0;function updateMsgOnBrowser() {	eval("var retval = " + req.responseText);	var timestamp = retval.timestamp;	if (timestamp > lastPing) {			lastPing = timestamp;		var msg_display = document.getElementById("msg_display");				for (var i = 0; i < retval.messages.length; i++)		{			var tempdata = tempdata + "<br />" + retval.messages[i];		}		msg_display.innerHTML = tempdata;	}}

Share this post


Link to post
Share on other sites

I am now the happiest person in the world! :) :) :mellow: That code did the trick, but I did change how it added data to tempdata like Justsomeguy suggested.THANKS Synook and Justsomeguy!

Good judgement comes from experience, and experience comes from bad judgement.~Fred Brooks

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...