Jump to content

jQuery.get() JSON request


The Letter E

Recommended Posts

I'm just learning the jquery ajax and still have a lot of questions, mainly because at this point it's not returning any results.

I was hoping someone could point out where i'm going wrong here.

 

 

Here's the main page:

    <!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>Ajax - PHP example</title>
	 <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
	<script  type="text/javascript">
	$(document).ready(function() {
			$("#work").click(function()
			{
					$.getJSON('remote.php?ajax=1&test=12', function(data) {
						$("#element").html(data.value);
					});   
			});
	});
</script>

    </head>
     
    <body>

		<div id="element"></div>
		<a href="#" id="work">DO WORK!</a>
    </body>
    </html>

 

and here's the php it's querying:

<?php
if(isset($_GET['ajax']) && $_GET['ajax'] == 1){
	$test = $_GET['test'];
	$array = array(12, 13, 34, 2, 18);
	$output = array();
	foreach($array as $check){
		$output[] .= $test * $check.'<br>';
	}
}

if(!empty($output)){
echo json_encode($output);
}else{
echo 0;
}
?>

 

This is just a test example to get my feet wet, and any help is appreciated.

 

Thanks,

 

E

Link to comment
https://forums.phpfreaks.com/topic/233150-jqueryget-json-request/
Share on other sites

Still unsolved?

 

Your php script is returning a JSON array but your javascript is looking for an object with a "value" property.

Changing $("#element").html(data.value); to $("#element").html(data[0]); will put "144" in the div.

 

Hopefully this will get you on the right track.

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.