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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.