javascript - How do I receive JSON array? -


i trying receive array php document in javascript using json. php document sends array (it printed when open php file browser) success() or done() methods not called. doing wrong here?

code:

javascript:

function updatehighscores() {      //var functionname = "showdata";     $.ajax({                 //type: "get",         datatype: "json",         url: "gesallprov.php",         //data: {functionname: functionname},     }).done(function(data) {         window.alert("gggg");          var table = "<table style='width:100%'>" +         "<tr>" +         "<th>no.</th>" +         "<th>name</th>" +         "<th>date & time</th>"  +         "<th>score</th>" +         "</tr>";          $(data).each(function(index, value) {                        var = 1;              table += "<tr>" +             "<td>" + + "</td>" +             "<td>" + value.name + "</td>" +              "<td>" + value.when + "</td>" +             "<td>" + value.score + "</td>" +             "</tr>";              i++;             }); } 

php:

function showdata() {     try {          $dbh = new pdo(...);         $st = $dbh->prepare("select `name` , `when` , `score` `snake` order `score` desc");         $st->execute();          $result = $st->fetchall(pdo::fetch_assoc);          return $result;         $st = null;         $dbh = null;     }     catch (pdoexception $ex) {         echo 'connection failed: ' . $ex->getmessage();     } }  echo json_encode(showdata()); 

why don't try following code might fine problem

     jquery.ajax({             type: "post",             url: "gesallprov.php",///contain url of ajax             // data:formdata,             datatype:"json",             success : function (data)                   {           window.alert("gggg");            var table = "<table style='width:100%'>" +           "<tr>" +          "<th>no.</th>" +          "<th>name</th>" +          "<th>date & time</th>"  +         "<th>score</th>" +          "</tr>";      $(data).each(function(index, value) {                    var = 1;          table += "<tr>" +         "<td>" + + "</td>" +         "<td>" + value.name + "</td>" +          "<td>" + value.when + "</td>" +         "<td>" + value.score + "</td>" +         "</tr>";          i++;         });           },       error :function(xhr, ajaxoptions, thrownerror){          console.log(xhr);         console.log(ajaxoptions);        // console.log(thrownerror);           //alert(xhr.status);           //alert(ajaxoptions);       }     }) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -