简体   繁体   中英

How to get data from json array in php via ajax get request?

I have 3 data in my database and I want my js to show all my data, But when I'm using json array, I get an error and my data doesn't show it all.

var confurl = "http://localhost:8080/servisppk/web_service.php";
$.ajax({
    url : confurl,
    type : 'GET',
    dataType: 'json',
    beforeSend : function() {
        $.mobile.loading('show', {
            text : 'please wait while retrieving data...',
            textVisible : true
        });
    },
    success : function(dataObject) {
        var appendList = '<li><a href="#page-two?id=' + dataObject.NIM + '" target="_self" id="detail-mhs" data-nimmhs="' + dataObject.NIM + '"><h2>' + dataObject.Nama + '</h2><p>' + dataObject.NIM + '</p><p><b>' + dataObject.Fakultas + '</b></P></a></li>';
        $('#list-mhs').append(appendList);
        $('#list-mhs').listview('refresh');
    },
    complete : function(){
        $.mobile.loading('hide');
    }
}); 

i'm using json array in my php:

$ms = array($mhs,$mhs2,$mhs3);
ECHO JSON_ENCODE($ms); 

when i'm using it, it doesn't show my data at all, it just show that my data is undefined, but when I change it to only be able to read one data, the data can appear even if only one.

changed to this:

$ms = $mhs;
ECHO JSON_ENCODE($ms);

You need send header let browser known, like:

<?php
$data = ['test' => 'hello'];
header('Content-Type: application/json');
echo json_encode($data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM