Prototype framework and JSON AJAX problem

I’m trying to make an AJAX request to my localhost server. Here is the code for the page that is requested, ajaxresponsecontent.php. The content is queried from a MySQL database then encoded into JSON using PHP.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="../javascript/prototype.js"></script>
<?php  

    @ $db = mysqli_connect('localhost', 'root', '', 'mealchamp');

	$query = "SELECT * FROM globalfood WHERE foodid = 1";
	$result = mysqli_query($db, $query);
	$row = mysqli_fetch_assoc($result);

	$reqVar = json_encode($row);
	$reqVar = "'" . $reqVar . "'";
	echo $reqVar;

?>
</head>
<body>
</body>
</html>

The output of the content page when run in a browser is serialized JSON:

‘{“foodid”:“1”,“food”:“Mozzarella cheese”,“brand”:“Black Diamond”,“ftype”:“Dairy & Alternatives”,“servamt”:“1\/4 inch cube”,“ss”:“30.0”,“cal”:“110.0”,“fat”:“8.0”,“sat”:“5.0”,“trans”:“0.2”,“chol”:“25.0”,“sod”:“230.0”,“carb”:“1.0”,“fib”:“0.0”,“sug”:“0.0”,“pro”:“8.0”}’

The second page, ajaxresponsecaller.php, makes an ajax request for this previous page using the Prototype framework. the code for the requesting page is:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="../javascript/prototype.js"></script>
<script type="text/javascript"><!--

new Ajax.Request('ajaxrequestcontent.php',
  {
    method:'get',
    onSuccess: function(transport){
      var response = transport.responseText || "no response text";
			document.write(response);
			var try1 = response.evalJSON();
			document.write(try1.food);
    },
    onFailure: function(){ alert('Something went wrong...') }
  });

--></script>
</head>
<body>
</body>
</html>

The PROBLEM is that the document.write(response) works fine in that it outputs the JSON string just how its supposed to be, same output as i showed above for the previous page. but when i try to use Prototypes’ evalJSON to convert it to an object, something is going wrong. document.write(try1.food); gives no output.

What am i doing wrong exactly? could u provide an example? been working at this for hours can’t figure out the problem.

Thanks so much, G

It looks like the ajaxrequestcontent.php page is also outputting HTML content. It should not be outputting any HTML content. It should only output JSON code.

i’m running this script on my testing server on local machine. From what i can tell get magic quotes it not turned on (code below is used to checK)


if(get_magic_quotes_gpc())
	echo "Magic quotes are enabled";
else
	echo "Magic quotes are disabled";

Anyways i use addslashes to escape apostraphes and slashes before inserting them into a mySQL database. However, when i try to unescape them using stripslashes, it doesnt seem to be having an effect.

the following is a snippet of code that acts on a MySQL query result. I’m attempting to stripslashes but it’s not working. When this query string is passed back to Javascript via Ajax, some things are still escaped when i output the JSON string to an alert box. For example, i call addslashes before inserting 1/2 into database, when i retrieve it through ajax via this script and use stripslashes, in an alert box it is still appearing as “1\/2”.


	if ( $returnVar->select == "yes" ) {
		$arr = NULL;

		if ($result)
			$num_results = mysqli_num_rows($result);
		else
			$num_results = 0;

		for ($i = 0; $i < $num_results; $i++) {
			$row = mysqli_fetch_assoc($result);
			foreach ( $row as $current ) {
				$current = stripslashes($current);
			}
			$arr[$i] = $row;
		} 
		$returnVar->items = $num_results;
		

	}

$z = rawurlencode(json_encode($returnVar));
echo $z;

i then retrieve $z using AJAX call.

Also, the string " Honey Nut O’s Cereal " is successfully inserted into database after calling addslashes on it. when its retrieved via the above snippet and output to a table cell in Html using JS, it appears the slash is gone. It also appears gone if i call an alert box on AJAX response string (but slash is sitll present in “1\/2” which i cannot figure out.) But when i then take the value from the table cell ( by using innerHTML ) and try to insert it back into the database, after calling addslashes it goes from having 0 slashes to 2 slashes!! wtf pls help cannot figure this out.