0

I have a function that return array object from php using mysql and i want call array from ajax function using javascript, but i don't know how display array object php in javascript dinamic table or console log....

my php file:

$age = $_POST["param"];  //value ajax param
$list = BussinesLayer::getUsers($age);  //load my list

//list properties ej:  list->getName(), getAge(), getOcupation()->getDescription(), etc..

echo json_encode($list); 

my js function:

 fnListUsers: function() {

    var agedata = $("#txtAge").val();

    $.ajax({
        type        : "POST",
        url         : "../ControllerFile/SearchUsers.php",
        data        : {"param" : agedata},
        dataType    : 'json',
        success     : global.fnSuccessList,
        error       : function (XMLHttpRequest, textStatus, errorThrown) {
                            alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
                      }
    });

},
fnSuccessList: function(data) {
    var array = JSON.parse(data); // -> dont work  Uncaught SyntaxError: Unexpected token o in JSON at position 1
    var array2 = jQuery.parseJSON(data);  // dont work  Uncaught SyntaxError: Unexpected token < in JSON at position 0

    //how display my arrayobject ?
    console.log(data.getName, data.getOcupation.getDescripition);

}
4
  • did you console.log(data) before using JSON.parse on it to see if you have any data in it? Commented Jun 11, 2017 at 6:37
  • im try with: console.log(data) and console show Commented Jun 11, 2017 at 6:58
  • im try with: console.log(data) and console show "Object {0: Object, 1: Object, 2: Object" Commented Jun 11, 2017 at 7:26
  • why are you parsing when it's already parsed dataType : 'json', this already done the job for you Commented Jun 11, 2017 at 7:42

2 Answers 2

1

Your success function already has the data in json format. So you don't need to parse it again in JSON. just use console.log(data) to see your object.

fnSuccessList: function(data) {
    var array = JSON.parse(data); // remove this
    var array2 = jQuery.parseJSON(data);  // remove this

    console.log(data);

}
Sign up to request clarification or add additional context in comments.

3 Comments

you must check his code, he is making mistake in his code.
@AjayDeepakKumar When did I say that he isn't making a mistake?? And your answer is exactly the same what I've said!!!
Oh my bad i did not see your answer. my mistake.
0

Here you are doing wrong you used dataType : 'json' that already parsed data but after that you are again try to parse. that is causing error.

fnListUsers: function() {

    var agedata = $("#txtAge").val();

    $.ajax({
        type        : "POST",
        url         : "../ControllerFile/SearchUsers.php",
        data        : {"param" : agedata},
        dataType    : 'JSON',
        success     : global.fnSuccessList,
        error       : function (XMLHttpRequest, textStatus, errorThrown) {
                            alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
                      }
    });

},
fnSuccessList: function(data) {

    // this will show your data
    console.log(data); 

    /* Don't use this just access data directly 
     var array = JSON.parse(data); // -> dont work  Uncaught SyntaxError: Unexpected token o in JSON at position 1
    var array2 = jQuery.parseJSON(data);  // dont work  Uncaught SyntaxError: Unexpected token < in JSON at position 0
   */


}

see in console if your data looks like this then it's parsed Parsed Image

if looks like this then it's not parsed enter image description here

4 Comments

try to see console.log(data) and see does that property is defined or not.
for example, the property getName() is undefined, but console log show this: Object 0 : Object proto : Object 1 : Object proto : Object 2 : Object proto : Object proto : Object
{"0":{},"1":{},"2":{}} is strange, becouse i test list in php file and list have values :/ imgur.com/a/N3RsA
that does not make any sense you are trying to access value which is not even exist

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.