Kieron606
Kieron606

Reputation: 613

Returning data from PHP response?

My AJAX request is this -

    $.ajax({
        type: "POST",
        url: "ajax/feed-check.php",
        dataType: "json",       
        data: {
            server: server,
        },  
        complete: function(data) {
            console.log(data);
            $('.Agencies').html('<p>'+data[0]+'</p>');
        }           
    })

The above returns and array like this below

[{"feed_name":"example.zip","feed_time":"2015-10-16 00:00:24","feed_size":"1222","back_office"
:"example4","agencyID":"example2"},{"feed_name":"example2.zip","feed_time":"2015-10-16 08:20:00","feed_size"
:"3145","back_office":"example1","agencyID":"aaa"}]
    "

How do i get the data out when the complete function is done in the AJAX request I am trying to do it like this

        complete: function(data) {
            $('.Agencies').html('<p>'+data[0]+'</p>');
        }           

But i am getting undefined, can someone tell me where i am going wrong? I need to get all of the data out.

My PHP script -

        $whereArray = array(
            "$where",
            "=",
            $_POST['server'],
        );

        $andArray = array(); //- Blank 'AND' array so that the 'get' call below doesn't fail if no 'ANDs' are passed.
        $orArray = array(); //- Blank 'OR' array so that the 'get' call below doesn't fail if no 'ORs' are passed.
        $order = array();

        $agencyfeed = paddyDB::getInstance("paddy_ms")->get('feed_files', $whereArray, $andArray, $orArray, $order);

        //print_r ($agencyfeed->results());
        $feeds = [];
        foreach ($agencyfeed->results() as $key) {

            $feeds[] = $key;
            //$key = $feeds['feed_name'];

        }

        echo json_encode($feeds);

Was looking at the wrong file, derp. Have updated the post with the relevant details

Thank you.

Upvotes: 1

Views: 94

Answers (4)

Kieron606
Kieron606

Reputation: 613

Thanks for all the help, I've found a solution now

        success: function(data) {
            $.each(data,function(k,v){
            var serverCont =  '<tr>';
                serverCont += '<td>'+v.feed_name+'</td>';
                serverCont += '<td>'+v.feed_date+'</td>';
                serverCont += '<td>'+v.feed_time+'</td>';
                serverCont += '<td>'+v.feed_size+'kb</td>';
                serverCont += '<td>'+v.agencyID+'</td>';
                serverCont += '</tr>';      
                thisServer.append(serverCont);                      
            });
        },

Upvotes: 0

Sashant Pardeshi
Sashant Pardeshi

Reputation: 1095

Please try code below. I have used $.each function nested to parse your json object. Please refer jsfiddle - https://jsfiddle.net/sashant9/npq9efur/2/

** Please let me know, If your desired output is different.

$.ajax({
        type: "POST",
        url: "ajax/feed-check.php",
        dataType: "json",       
        data: {
            server: server,
        },  
        complete: function(data) {
            $.each(data , function(index, value){
                  $.each(value, function(ind,val){
                        $('.Agencies').append('<p>'+val+'</p>');    
                  });
           });
        }           
    });

Output -->

example.zip

2015-10-16 00:00:24

1222

example4

example2

example2.zip

2015-10-16 08:20:00

3145

example1

aaa

Upvotes: 1

Mahesh Jagdale
Mahesh Jagdale

Reputation: 174

Try this:

complete: function(data) {
                $.each(data, function(i, member) 
                {
                    $(".Agencies").html('<p>'+data[i].feed_name+'</p>');
                })

            }

Upvotes: 2

Styphon
Styphon

Reputation: 10447

Have you told your ajax request to expect a JSON response? If not then it doesn't decode the JSON and you just end up with a string. Use console.log(data) to check, you'll probably get a string.

You can get jQuery to automatically convert the JSON into an object by setting dataType: "JSON" in the jQuery.ajax options.

Upvotes: 1

Related Questions