How do I encode JSON in PHP via jQuery Ajax post data?

前端 未结 5 806
南旧
南旧 2021-01-31 00:16

I have an HTML form and send data to php file when hitting submit button.

$.ajax({
    url: \"text.php\",
    type: \"POST\",
    data: {
        amount: amount,         


        
5条回答
  •  广开言路
    2021-01-31 01:19

    You're converting resulting object to string instead of displaying it.

    Instead of result, if you want print object inside some wrapper, you can do something like this:

    var text = '{';
    
    for(var i in data) {
      var value = data[i];
    
      text += '"'+i+'":"'+value+'", ';
    }
    
    text += '}';
    
    $('#result').text(text);
    

    But you may consider that console.log is much easier and faster way to see the response in json format.

提交回复
热议问题