working with JSON data array from Ajax response?

后端 未结 5 1692
一个人的身影
一个人的身影 2020-12-21 21:26

Is it possible to work with a response from AJAX request in PHP? I am not really a JS dev so i am polling my hair out with this one.

I have sort of hacked this toget

5条回答
  •  被撕碎了的回忆
    2020-12-21 21:51

    You need to use json_encode() in your php file to send the data back as an array

    For example;

    $myarray = array("data1"=>"value1","data2"=>"value2");
    
    echo json_encode($myarray);
    

    You can then access the data separately in the js file like this;

     success: function(html) { 
        alert(html.data1);
        alert(html.data2);
      },
    

    You also need to change the dataType to 'json'

提交回复
热议问题