0

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"}
10
  • 1
    That does not seem like JSON to me. Commented Feb 6, 2014 at 15:09
  • Please post value of data, not pseudo code or whatever it is Commented Feb 6, 2014 at 15:10
  • @Martin this isn't a valid JSON. For more information, go to JSON's Official Website Commented Feb 6, 2014 at 15:10
  • don't use alert, use console.log. and, you should console.log data to see that it contains what you think it should. Commented Feb 6, 2014 at 15:11
  • Try this and tell what you are getting. alert(JSON.stringify(data)); Commented Feb 6, 2014 at 15:13

3 Answers 3

2

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    
});
Sign up to request clarification or add additional context in comments.

1 Comment

A better and elegant solution. Nice Dennis. +1
0

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"); 
        }
    }
  });

1 Comment

You won't have data.status if you use the 'success' callback unless you have specified it in your server side generated JSON
0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.