在jQuery中解析JSON数据并在textfield中显示

时间:2015-12-17 07:20:32

标签: javascript jquery json

我打算以JSON格式打印来自test.php的响应数据,以便在特定字段上打印

$.ajax({
    type: 'POST',
    url: 'test.php',
    data: data,
   success: function(response) {
   var result = $.parseJSON(response);

   $(document).ready(function(){
      $("#test").click(function(){
          $("#bemail").val(result.email);//when i prints only result than it displays [object object]

      });
   });
   }
});

3 个答案:

答案 0 :(得分:0)

你在AJAX成功处理程序中调用document.ready(),因为AJAX调用没有再次调用文档加载,DOM已经加载并且它在生命中只加载一次页面会话的循环。

这应该做的很多

          $.ajax({
                type: 'POST',
                url: 'test.php',
                data: data,
               success: function(response) {
                  var result = JSON.parse(response);
                  $("#bemail").val(result[0].email); //after you explained the JSON response
               }
            });

答案 1 :(得分:0)

试试这样。你必须将你的ajax放在$(document).ready

$(document).ready(function(){
$.ajax({
                type: 'POST',
                url: 'test.php',
                data: data,
               success: function(response) {
                  var result = JSON.parse(response);
                  $("#bemail").val(result.email);
               }
            });

});

答案 2 :(得分:0)

您的代码完全错误,应该是

function displayEmail() {
  $.ajax({
    type: 'POST',
    url: 'test.php',
    data: data,
    success: function(response) {
      var result = $.parseJSON(response);
      //Just Print the Result in Console using console.log(result)
      $("#bemail").val(result.email);
    }
  });
}
$(document).ready(function() {
  $("#test").click(function() {
    displayEmail();
  });
});
相关问题