How to pass an array using PHP & Ajax to Javascript?

后端 未结 5 1348
执笔经年
执笔经年 2020-12-03 07:33

Apologies if this explanation isn\'t clear, it\'s hard for me to understand too. How can I use PHP & Ajax to send an array to Javascript? I\'m using Aja

5条回答
  •  攒了一身酷
    2020-12-03 08:07

    Try:

    $.ajax({
        url: "",
        type: 'POST',
        data: form_data,
        dataType:"json",
        success: function(data) {
            alert(data[0]);
       }
    

    On the PHP side, you'll be wanting to print:

    print json_encode($photos);
    

    Another thing you might try in order to better encapsulate your code, and as an example of further JSON gooodness, would be:

    print json_encode(array("photolist"=>$photos,"photo_owner"=>"Me!"));
    

    Then on the server, you'd access these with:

    data.photolist[0]; //First photo
    data.photo_owner;  //The owner of the photo set
    

提交回复
热议问题