2 Replies - 1209 Views - Last Post: 18 April 2013 - 10:57 AM

#1 squibby  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 82
  • Joined: 21-January 12

Cannot get JSON object after AJAX call

Posted 17 April 2013 - 01:42 PM

Been struggling with this for days - hope someone can enlighten me.

After clicking on a button an ID is sent to a PHP script via ajax - the script uses the id to search my database and echos out a JSON encodedd string. On success i want to then print that information to my user. However i only get 'object object' returned in an alert. The JSON is showing up in the response of firebug and is valid JSON as i checked in a validator.

JQUERY CODE

<script>

    $(document).ready(function() {

        // add listener to all link
        $(".edit-customer").click(function(mouseEvent) {
		
            // Retrieve the customer ID                
            var Id = $(this).attr('id');

            // Ajax call with the id of the link
            $.ajax('ajax-get-profile.php', {
				
		dataType: "json",
                type: 'POST',
                data: { cust_id: Id },
                success: function(data) {
				
                    // The data is received, update the info container
                   //$("#profile-modal input[name=name]").val(data.cust_business_name);
                   //$("#profile-modal[name=contact]").val(data.cust_contact_name);

				   
                    // Show the div in a popup
                    //...
			//$("#profile-modal").show();	
					
			alert(data);
					
                }
				
				
            });

            return false;
        });
    });

</script>





PHP CODE


<?php

//at the very beginning start output buffereing
ob_start();
$cust_id = $_POST['cust_id'];

include('connect.php');
$sql = "SELECT * FROM customers WHERE cust_id = $cust_id";
$result = mysql_query($sql) or die ('Error with Query');
$rows = array();
 while($r = mysql_fetch_assoc($result)) {
     $rows[] = $r;
   }

// right before outputting the JSON, clear the buffer.
ob_end_clean();
echo json_encode($rows);

?>






JSON CODE



[{"cust_id":"47","cust_business_name":"Smiths","cust_contact_name":"Andrew Smith","cust_address":"5 high street","cust_town":"manchester","cust_postcode":"MN6 78P","cust_tel":"245645654356","cust_mobile":"3463463456456","cust_email":"smith@whatever.com","cust_join_date":"0000-00-00","cust_last_visit":"0000-00-00","cust_payment_status":"YES","cust_have_website":"NO","cust_status":""}]





Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Cannot get JSON object after AJAX call

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3393
  • View blog
  • Posts: 9,592
  • Joined: 08-June 10

Re: Cannot get JSON object after AJAX call

Posted 17 April 2013 - 01:51 PM

Quote

However i only get 'object object' returned in an alert.

because that is what an object returns when you convert it to a string (an alert() does that). if you want it in a more readable form, you need to stringify the object first.

more in-dephth explanation: and function or call that tries to somehow print an object (like alert(), or a text assignment like innerHTML) calls that object’s toString() method. if the object doesn’t provide such a method itself, the toString() method of its prototype is used. since all objects inherit from Object, thats method is used as last fallback which goes similar to
Object.toString = function()
{
    return "[" + typeof this + " " + this.constructor + "]";
}

This post has been edited by Dormilich: 17 April 2013 - 01:54 PM

Was This Post Helpful? 1
  • +
  • -

#3 squibby  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 82
  • Joined: 21-January 12

Re: Cannot get JSON object after AJAX call

Posted 18 April 2013 - 10:57 AM

Thanks that got me going in the right direction.

I used a for loop to iterate over the object and extract the result like this:

 for (var i in data) {
     var customer = data[i];
     $('#test').show();
     $("#test").append(customer.cust_business_name);
     $("#test").append(customer.cust_contact_name);
    
 }






Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1