SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Uk
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery: extrating ajax returned JSON data for display

    I'm having trouble extracting ajax-returned data in JSON format. Here is my code:

    Code:
    $('#ajax').submit(function(e){
    		
    		$.ajax({
    			url: 'appajax',
    			type: 'POST',
    			data: { _date: $('.app_date').val() },
    			dataType: 'json',
    			success: function(info)
    			{
    
    				console.log(info);
    			}
    		});
    		e.preventDefault();
    	});
    When i run this code the console shows:

    Code:
    [Object]
    0: Object
    attributes: Object
    appointment_date: "2013-04-30"
    appointment_time: "18:15:00"
    created_at: "2013-04-19 23:14:16"
    doctor_id: 2
    id: 2
    patient_id: 3
    problem: "migraine"
    status: "unseen"
    updated_at: "2013-04-19 23:14:16"
    __proto__: Object
    exists: true
    includes: Array[0]
    original: Object
    relationships: Array[0]
    __proto__: Object
    length: 1
    __proto__: Array[0]
    ... and im able to see the object and all attributes. But when I go to console.log(info.appointment_date) for example, the console shows 'undefined'. What Im I missing? Or indeed how can I access individual attributes?

    I also tried this code inside the success method:

    Code:
    $.each(info, function(key, val) {
    					console.log(val);
    				});
    and it returns:

    Code:
    Object {attributes: Object, original: Object, relationships: Array[0], exists: true, includes: Array[0]}
    attributes: Object
    appointment_date: "2013-04-30"
    appointment_time: "18:15:00"
    created_at: "2013-04-19 23:14:16"
    doctor_id: 2
    id: 2
    patient_id: 3
    problem: "migraine"
    status: "unseen"
    updated_at: "2013-04-19 23:14:16"
    __proto__: Object
    exists: true
    includes: Array[0]
    original: Object
    relationships: Array[0]
    __proto__: Object
    And still im unable to extract the data. What do i do? Thanks

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,881
    Mentioned
    208 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    instead of:

    Code JavaScript:
    console.log(info.appointment_date)

    try:

    Code JavaScript:
    console.log(info['appointment_date'])

    or failing that:

    Code JavaScript:
    console.log(info[0]['appointment_date'])

    Does that work?

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Uk
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The last option works perfectly i.e.

    Code:
    console.log(info[0]['appointment_date'])
    Many thanks...

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,881
    Mentioned
    208 Post(s)
    Tagged
    12 Thread(s)
    You're welcome.
    Thanks for taking the time to report back


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •