Jump to content

Displaying imported message


dcole.ath.cx

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;	}}

Link to comment
Share on other sites

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

Link to comment
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

Link to comment
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.

Link to comment
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.

Link to comment
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);}

Link to comment
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;	}}

Link to comment
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
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...