Martin Fric
Martin Fric

Reputation: 730

printing json object with jquery

I have problem with printing json object. In firebug i can see this is returned from ajax call.

 ABOUT
"Company"

CONTACT
"Contacts"

FAKTY
"Facts"

KARIERA
"Career"

etc.

this is my ajax call

 $.ajax({
    url: 'defines/defines_en.php',
    dataType: 'json',
    complete: function (data) { 
        if (data.status == 200){
            alert(data['ABOUT']);
            alert(data.ABOUT);
        }
        else {
            alert("Nepodarilo sa zmeniť jazyk"); 
        }
    }
  });

It gives me undefined in both of those alerts.

Can someone help?

UPDATE defines_en.php file :

 <?php
 $data = array(
'PORTFOLIO'=>'Portfolio',
'ABOUT'=>'Company',
'CONTACT'=>'Contacts',
'ZALOHOVANIE'=>'Backup and Archivation',
'KONSOLIDACIA'=>'Consolidation and Virtualization',
'MANAZMENT'=>'Management and Monitoring',
'NETWORKSEC'=>'Network security',
'SPRAVAKONC'=>'Správa koncových zariadení',
'FAKTY'=>'Facts',
'MGMT'=>'Management',
'REF'=>'References',
'KARIERA'=>'Career',
 );
 echo json_encode($data);
 ?>

UPDATE 2 alert(JSON.stringify(data)) :

 {"readyState":4,"responseText":"{\"PORTFOLIO\":\"Portfolio\",\"ABOUT\":\"Company\",\"CONTACT\":\"Contacts\",\"ZALOHOVANIE\":\"Backup and Archivation\",\"KONSOLIDACIA\":\"Consolidation and Virtualization\",\"MANAZMENT\":\"Management and Monitoring\",\"NETWORKSEC\":\"Network security\",\"SPRAVAKONC\":\"Spr\\u00e1va koncov\\u00fdch zariaden\\u00ed\",\"FAKTY\":\"Facts\",\"MGMT\":\"Management\",\"REF\":\"References\",\"KARIERA\":\"Career\"}","responseJSON":{"PORTFOLIO":"Portfolio","ABOUT":"Company","CONTACT":"Contacts","ZALOHOVANIE":"Backup and Archivation","KONSOLIDACIA":"Consolidation and Virtualization","MANAZMENT":"Management and Monitoring","NETWORKSEC":"Network security","SPRAVAKONC":"Správa koncových zariadení","FAKTY":"Facts","MGMT":"Management","REF":"References","KARIERA":"Career"},"status":200,"statusText":"OK"}

Upvotes: 0

Views: 114

Answers (3)

Mir
Mir

Reputation: 1613

There is a difference in jquery between the 'complete' callback and the 'success' one.

If you use 'complete' then the first argument passed is a jqXHR object and not directly the data you want. So you have all your properties undefined.

If you use the 'success' callback then you have the actual data as first argument and you can use them as you are. (except for the fact that you will not have a 'status' property)

that said:

$.ajax({
url: 'defines/defines_en.php',
dataType: 'json',
success: function (data) { 
       alert(data.ABOUT);
},
error: function(error) {
  alert('error message here')
}});

documentation here : http://api.jquery.com/jquery.ajax/

Upvotes: 0

Dennis Martinez
Dennis Martinez

Reputation: 6512

complete won't return your json object, it only returns the xhr request and status text. try using success or done.

// one way
$.ajax({
    url: 'defines/defines_en.php',
    dataType: 'json',
    success: function (data) {
        alert(data['ABOUT']);
        alert(data.ABOUT);
    },
    error: function (e) {
        alert("Nepodarilo sa zmeniť jazyk");
    }
});


// another way
$.getJSON('url')
.done(function (e) {
    // success   
})
.fail(function (e) {
    // error    
});

Upvotes: 2

Shiva Avula
Shiva Avula

Reputation: 1836

You have to get the responseText from the data object. Try this.

$.ajax({
    url: 'defines/defines_en.php',
    dataType: 'json',
    complete: function (data) { 
        if (data.status == 200){
            response = data.responseText;
            //alert(data['ABOUT']);
            alert(response.ABOUT);
        }
        else {
            alert("Nepodarilo sa zmeniť jazyk"); 
        }
    }
  });

Upvotes: 0

Related Questions