• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

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!
0
hibbsusan
Asked:
hibbsusan
  • 5
  • 5
1 Solution
 
leakim971PluritechnicianCommented:
Use :

$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


client side :
  			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>
  });

Open in new window

0
 
hibbsusanAuthor Commented:
	$(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
0
 
leakim971PluritechnicianCommented:
could you provide a link to your page?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
hibbsusanAuthor Commented:
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

0
 
leakim971PluritechnicianCommented:
could you provide a link to your page?
0
 
hibbsusanAuthor Commented:
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

0
 
leakim971PluritechnicianCommented:
if look my code ID:37060449

I did NOT put the header and the next line inside the while loop!
0
 
hibbsusanAuthor Commented:
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

0
 
leakim971PluritechnicianCommented:
:))

what about the credentials?
0
 
hibbsusanAuthor Commented:
this worked so smoothly! it's going great!

five-hundred'd!

thank you so much :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now