Link to home
Start Free TrialLog in
Avatar of hibbsusan
hibbsusan

asked on

getJSON returning oddly even though seems to be formatted correctly

Hello!

When I'm trying to use getJSON, no results are returned at all.

and when i use what the jquery.net seems to indicate is just the lengthened version:
 function get()
	  {
			$.ajax({
  			url: 'address_process.php',
  			success: function(data) {
			var items = [];

  $.each(data, function(key, val) {
    items.push('<li id="' + key + '">' + val + '</li>');
  });

  $('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo('body');
  }
});

Open in new window


I get an unordered list returned with one character from the array per bullet point. Sample:

[•{
•"
•a
•"
•:
•"
•s
•u
•s
•a
•n
•h
•i
•b
•b
•a
•r
•d
•"
•,
•"
•b
•"
•:
•"
•I
•n
•d



address_process_php
while ($row = mysql_fetch_array($result)) 
{
echo json_encode(array("a" => $row['attn'], "b" => $row['company_name']));
}	

Open in new window



This seems to work okay because when I go to address_process.php, I get:
{"a":"susan hibbard","b":"Independent's Service"}{"a":"susan hibbard","b":"isco"}{"a":"susan","b":"indep"}


Many thanks for any insight!
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hibbsusan
hibbsusan

ASKER

	$(document).ready(function() {
		$("#add_address_btn").click(function()
		{
		
		  $.ajax ({
			type: "POST",
			url: "address_process.php",
			success: function(data)
			{
				var items = [];
				$.each(data, function(key, val) {
   				 items.push('<li id="' + val.a + '">' + val.b + '</li>'); // ie : <li id="1234">Experts Exchange</li>
 				 });
				 $('<ul/>', {
    			'class': 'my-new-list',
    			html: items.join('')
  				}).appendTo('body');
  			}
			});
		});
	});

Open in new window


prints to screen:


      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
      •      undefined
could you provide a link to your page?
actually, i forgot to upload the new php info before I posted that. After I make that change, nothing happens at all.

$arr = array(); // create an empty array
while ($row = mysql_fetch_array($result)) {
$arr[] = array("a" => $row['attn'], "b" => $row['company_name'] ); // push all object inside the array
header("Content-type: application/json"); // set the header, it's safe
echo json_encode($arr); // encode the full object to a json string object

Open in new window

could you provide a link to your page?
and when I go to the address_process.php page, I see the folowing

[{"a":"susan hibbard","b":"Independent's Service"}]<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/iscosand/public_html/php/RegistrationForm/source/address_process.php:37) in <b>/home/iscosand/public_html/php/RegistrationForm/source/address_process.php</b> on line <b>36</b><br />
[{"a":"susan hibbard","b":"Independent's Service"},{"a":"susan hibbard","b":"isco"}]<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/iscosand/public_html/php/RegistrationForm/source/address_process.php:37) in <b>/home/iscosand/public_html/php/RegistrationForm/source/address_process.php</b> on line <b>36</b><br />
[{"a":"susan hibbard","b":"Independent's Service"},{"a":"susan hibbard","b":"isco"},{"a":"susan","b":"indep"}]

Open in new window


but I'm not aware of having sent any header information; this is the entire page:

<?php
session_start();
$u = $_SESSION['u'];

$con = mysql_connect('localhost', 'iscosand_admin', '123');
	if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

	mysql_select_db("iscosand_login1", $con);
	if (!$con)
	  {
	  die(mysql_error());
	  }
	
	$user = mysql_query("SELECT id_user FROM users WHERE email='".$u."'");
	while ($row = mysql_fetch_array($user)) 
	{
    $id_user = $row['id_user'];
	}


	$result = mysql_query("SELECT * FROM billing_address WHERE id='".$id_user."'");
	//$temp = mysql_num_rows($result);
	//echo 'temp is '.$temp;
if (!$result)
{
	die(mysql_error());
	}
	else
	{ 
	$arr = array(); // create an empty array
	while ($row = mysql_fetch_array($result)) 
	{
	$arr[] = array("a" => $row['attn'], "b" => $row['company_name'] ); // push all object inside the array
	header("Content-type: application/json"); // set the header, it's safe
	echo json_encode($arr); // encode the full object to a json string object
	}
}

?>

Open in new window

if look my code ID:37060449

I did NOT put the header and the next line inside the while loop!
no! you did not! :) sry.

and i posted those so quickly i didn't see your multiple requests for a link to the page..

http://www.iscosandbox.com/php/RegistrationForm/source/newadd.php

:))

what about the credentials?
this worked so smoothly! it's going great!

five-hundred'd!

thank you so much :)