Jump to content

Eval() Not Working


ShadowMage

Recommended Posts

Hello again,I'm trying to use eval() on a JSON string. Problem is my function keeps aborting on the eval line. I don't know what's wrong.Here's the code:

httpRequest.onreadystatechange = function() {	if(httpRequest.readyState == 4) {		//If the tip doesn't exist, create it		if (!homePage.getElementById("miscCharge_tip")) {			divTipNode = homePage.createElement('div');			divTipNode.style.width = "24em";			divTipNode.className = "tip";			divTipNode.id = "miscCharge_tip";						tmpNode = homePage.createElement('div');			tmpNode.className = "tip_title";			tmpNode.innerHTML = "Other Charges";			divTipNode.appendChild(tmpNode);						tmpNode = homePage.createElement('div');			tmpNode.className = "tip_body";			divTipNode.appendChild(tmpNode);		}		divContainer = homePage.getElementById("miscChrg_tipBody");				//arrResponse = eval("("+httpRequest.responseText+")");		tmpResponseTxt = httpRequest.responseText;		arrResponse = eval("("+tmpResponseTxt+")");		divContainer.innerHTML = arrResponse.HTML;		homePage.getElementById("OtherCost").value = FormatNumber(arrResponse.Total, 2);				//window.close();	}}

In case you haven't guessed this is also an AJAX request, but the AJAX seems to be working fine.It's this line that's aborting:arrResponse = eval("("+tmpResponseTxt+")");Here is the JSON string I'm trying to eval():

"{"HTML": "<table style='width: 350px; font-size: 14px; border-style: groove;'>\n<tr><th style='width: 75%;'>Description</th>\n<th colspan='2'>Value</th></tr>\n<tr><td style='text-align: right; border-bottom: dashed; border-width: 1px;'><input name='MiscChrgDesc' id='MiscChrgDesc0' class='tableInfo chargeDesc' value='Charge 1' readonly /></td>\n<td style='width: 10px; text-align: right; border-bottom: dashed; border-width: 1px;'>$</td>\n<td style='text-align: right; border-bottom: dashed; border-width: 1px;'><input name='MiscChrgAmt' id='MiscChrgAmt0' class='tableInfo' value='900' style='width: 40px;' readonly /></td></tr>\n<tr><td style='text-align: right; border-bottom: dashed; border-width: 1px;'><input name='MiscChrgDesc' id='MiscChrgDesc1' class='tableInfo chargeDesc' value='Charge 2' readonly /></td>\n<td style='width: 10px; text-align: right; border-bottom: dashed; border-width: 1px;'>$</td>\n<td style='text-align: right; border-bottom: dashed; border-width: 1px;'><input name='MiscChrgAmt' id='MiscChrgAmt1' class='tableInfo' value='750' style='width: 40px;' readonly /></td></tr>\n<tr><td style='text-align: right; border-bottom: dashed; border-width: 1px;'><input name='MiscChrgDesc' id='MiscChrgDesc2' class='tableInfo chargeDesc' value='Bleu cheese and taco sauce' readonly /></td>\n<td style='width: 10px; text-align: right; border-bottom: dashed; border-width: 1px;'>$</td>\n<td style='text-align: right; border-bottom: dashed; border-width: 1px;'><input name='MiscChrgAmt' id='MiscChrgAmt2' class='tableInfo' value='1,322' style='width: 40px;' readonly /></td></tr>\n<tr><td class='header' style='border-bottom: none;'>Total</td>\n<td style='font-weight: bold; border-bottom: none;'>$</td>\n<td class='header' style='border-bottom: none;'>2,972</td></tr>\n</table>\n", "Total": 2972}"

Any help is appreciated, thank you.

Link to comment
Share on other sites

1. You need to escape some quote marks inside your string.2. Be aware that the quotes around object member identifiers is optional in javascript.3. You should assign the object to a variable right inside the string.In PHP, your server side would look (we'll pretend) like this:echo "myObj={HTML: \"<table style . . .When you access the data in the client, look for it thus:eval(httpRequest.responseText);divContainer.innerHTML = myObj['HTML'];homePage.getElementById("OtherCost").value = myObj['Total'];

Link to comment
Share on other sites

I've tried to change the things you guys suggested, but it still aborts the script at the same spot. Firebug doesn't give me any error messages, so that's not much help.Here is the new string:

"arrResponse = {HTML: \"<table style=\'width: 350px; font-size: 14px; border-style: groove;\'><tr><th style=\'width: 75%;\'>Description</th><th colspan=\'2\'>Value</th></tr><tr><td style=\'text-align: right; border-bottom: dashed; border-width: 1px;\'><input name=\'MiscChrgDesc\' id=\'MiscChrgDesc0\' class=\'tableInfo chargeDesc\' value=\'Charge 1\' readonly /></td><td style=\'width: 10px; text-align: right; border-bottom: dashed; border-width: 1px;\'>$</td><td style=\'text-align: right; border-bottom: dashed; border-width: 1px;\'><input name=\'MiscChrgAmt\' id=\'MiscChrgAmt0\' class=\'tableInfo\' value=\'900\' style=\'width: 40px;\' readonly /></td></tr><tr><td style=\'text-align: right; border-bottom: dashed; border-width: 1px;\'><input name=\'MiscChrgDesc\' id=\'MiscChrgDesc1\' class=\'tableInfo chargeDesc\' value=\'Charge 2\' readonly /></td><td style=\'width: 10px; text-align: right; border-bottom: dashed; border-width: 1px;\'>$</td><td style=\'text-align: right; border-bottom: dashed; border-width: 1px;\'><input name=\'MiscChrgAmt\' id=\'MiscChrgAmt1\' class=\'tableInfo\' value=\'750\' style=\'width: 40px;\' readonly /></td></tr><tr><td style=\'text-align: right; border-bottom: dashed; border-width: 1px;\'><input name=\'MiscChrgDesc\' id=\'MiscChrgDesc2\' class=\'tableInfo chargeDesc\' value=\'Bleu cheese and taco sauce\' readonly /></td><td style=\'width: 10px; text-align: right; border-bottom: dashed; border-width: 1px;\'>$</td><td style=\'text-align: right; border-bottom: dashed; border-width: 1px;\'><input name=\'MiscChrgAmt\' id=\'MiscChrgAmt2\' class=\'tableInfo\' value=\'1,322\' style=\'width: 40px;\' readonly /></td></tr><tr><td class=\'header\' style=\'border-bottom: none;\'>Total</td><td style=\'font-weight: bold; border-bottom: none;\'>$</td><td class=\'header\' style=\'border-bottom: none;\'>2,972</td></tr></table>\", Total: 2972};"

Link to comment
Share on other sites

It still isn't working. I put escaped quotes around the property names like so:arrResponse = {\"HTML\": \"<table> ...Here is the PHP code:

echo "arrResponse = {\\\"HTML\\\": \\\"".addslashes($html)."\\\", \\\"Total\\\": ".$Total."};";

And here's the updated JS:

eval("("+httpRequest.responseText+")"); //This is the line where the script abortsdivContainer.innerHTML = arrResponse["HTML"];homePage.getElementById("OtherCost").value = FormatNumber(arrResponse["Total"], 2);

Link to comment
Share on other sites

var json = "{\"HTML\": \"<table style='width: 350px; font-size: 14px; border-style: groove;'><tr><th style='width: 75%;'>Description</th><th colspan='2'>Value</th></tr><tr><td style='text-align: right; border-bottom: dashed; border-width: 1px;'><input name='MiscChrgDesc' id='MiscChrgDesc0' class='tableInfo chargeDesc' value='Charge 1' readonly /></td><td style='width: 10px; text-align: right; border-bottom: dashed; border-width: 1px;'>$</td><td style='text-align: right; border-bottom: dashed; border-width: 1px;'><input name='MiscChrgAmt' id='MiscChrgAmt0' class='tableInfo' value='900' style='width: 40px;' readonly /></td></tr><tr><td style='text-align: right; border-bottom: dashed; border-width: 1px;'><input name='MiscChrgDesc' id='MiscChrgDesc1' class='tableInfo chargeDesc' value='Charge 2' readonly /></td><td style='width: 10px; text-align: right; border-bottom: dashed; border-width: 1px;'>$</td><td style='text-align: right; border-bottom: dashed; border-width: 1px;'><input name='MiscChrgAmt' id='MiscChrgAmt1' class='tableInfo' value='750' style='width: 40px;' readonly /></td></tr><tr><td style='text-align: right; border-bottom: dashed; border-width: 1px;'><input name='MiscChrgDesc' id='MiscChrgDesc2' class='tableInfo chargeDesc' value='Bleu cheese and taco sauce' readonly /></td><td style='width: 10px; text-align: right; border-bottom: dashed; border-width: 1px;'>$</td><td style='text-align: right; border-bottom: dashed; border-width: 1px;'><input name='MiscChrgAmt' id='MiscChrgAmt2' class='tableInfo' value='1,322' style='width: 40px;' readonly /></td></tr><tr><td class='header' style='border-bottom: none;'>Total</td><td style='font-weight: bold; border-bottom: none;'>$</td><td class='header' style='border-bottom: none;'>2,972</td></tr></table>\", \"Total\": 2972}";var obj = eval("(" + json + ")");

That's your original string, with the quotes around the property names escaped, and the newlines removed. That code works.Incidentally, why aren't you using json_encode from PHP to generate the JSON string?

Link to comment
Share on other sites

You can save this file as class.services_json.php:http://mike.teczno.com/JSON/JSON.phpsAnd then use this to define the functions if they don't exist:

if ( !function_exists('json_decode') ){  function json_decode($content, $assoc=false)  {	require_once 'class.services_json.php';	if ($assoc)	{	  $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);	}	else	{	  $json = new Services_JSON;	}	return $json->decode($content);  }}if ( !function_exists('json_encode') ){  function json_encode($content)  {	require_once 'class.services_json.php';	$json = new Services_JSON;	return $json->encode($content);  }}

That will define json_encode and json_decode if they don't exist.

Link to comment
Share on other sites

I got it working now. I had to tweak my escape characters.In my PHP I was using this \\\" to print an escaped quote mark. It should have been just \" so it prints a quote mark.Thank all of you for your help. I very much appreciate you guys putting up with me. :)

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...