使用Ajax Get方法在前端获取MongoDB数据

Posted

技术标签:

【中文标题】使用Ajax Get方法在前端获取MongoDB数据【英文标题】:Retrieving Mongodb data in front end by using Ajax Get Method 【发布时间】:2021-11-25 10:26:59 【问题描述】:

我在 mongodb 中插入了一个数据并使用 nodejs 编写 API,需要使用 jquery 在前端检索这些数据。我在 mongodb 中插入了 3 行数据。我使用下面的代码来获取数据,它工作正常,但它是硬编码的。我希望它使用 jquery 手动发生。请帮忙解决这个问题。

 $.ajax(
        dataType:"json",
        url: "/purchaser/purchasersList",
        type:"GET",
        global:false,
        async:false,
        success: function(response)
            console.log("response is:",response);
            document.getElementById("userName").innerhtml = (response[0].userName);
            document.getElementById("email_id").innerHTML=(response[0].email_id);
            document.getElementById("address").innerHTML=(response[0].address);
            document.getElementById("phoneNumber").innerHTML=(response[0].phoneNumber);
              //2nd row data
            document.getElementById("userName1").innerHTML = (response[1].userName);
            document.getElementById("email_id1").innerHTML=(response[1].email_id);
            document.getElementById("address1").innerHTML=(response[1].address);
            document.getElementById("phoneNumber1").innerHTML=(response[1].phoneNumber);
             //3rd row data
            document.getElementById("userName2").innerHTML = (response[2].userName);
            document.getElementById("email_id2").innerHTML = (response[2].email_id);
            document.getElementById("address2").innerHTML = (response[2].address);
            document.getElementById("phoneNumber2").innerHTML =(response[2].phoneNumber);
        
         ,


 error: function (jqXHR, textStatus, errorThrown)  // error callback    

             console.log("Error Response jqXHR is:" + jqXHR);e
<table class = table2>
  <tr>
    <th  style="text-align:center">SL.No</th> 
    <th style="text-align:center">Purchaser Name</th>
    <th style="text-align:center">Email</th> 
    <th style="text-align:center">Address</th>
    <th style="text-align:center">Phone No</th>
   </tr> 
   <tr>
  
    <td >1</td>
       <td  id="userName"></td>
       <td  id="email_id"></td>
       <td  id="address"></td>
       <td  id="phoneNumber"></td>
       <td >Active</td>
  </tr> 
  <tr>
  ..
  </tr>

【问题讨论】:

【参考方案1】:

如果您可以将 id 更改为 class,那么我鼓励您尝试以下操作:

$.each(response,function(i) 
  var tr = $('.table2 tr').eq((i+1));
  $(tr).find(".userName").text(response[i].userName)
  $(tr).find(".email_id").text(response[i].email_id)
  $(tr).find(".address").text(response[i].address)
  $(tr).find(".phoneNumber").text(response[i].phoneNumber)
)

请注意,我无法测试它,因为我没有收到您的回复。

完整代码

$.ajax(
      dataType: "json",
      url: "/purchaser/purchasersList",
      type: "GET",
      global: false,
      async: false,
      success: function(response) 
        $.each(response,function(i) 
          var tr = $('.table2 tr').eq((i+1));
          $(tr).find(".userName").text(response[i].userName)
          $(tr).find(".email_id").text(response[i].email_id)
          $(tr).find(".address").text(response[i].address)
          $(tr).find(".phoneNumber").text(response[i].phoneNumber)
        )

      ,


      error: function(jqXHR, textStatus, errorThrown)  // error callback    

          console.log("Error Response jqXHR is:" + jqXHR);
          e
<table class=table2>
  <tr>
    <th style="text-align:center">SL.No</th>
    <th style="text-align:center">Purchaser Name</th>
    <th style="text-align:center">Email</th>
    <th style="text-align:center">Address</th>
    <th style="text-align:center">Phone No</th>
  </tr>
  <tr>

    <td >1</td>
    <td  class="userName"></td>
    <td  class="email_id"></td>
    <td  class="address"></td>
    <td  class="phoneNumber"></td>
    <td >Active</td>
  </tr>
  <tr>
    ..
  </tr>

【讨论】:

以上是关于使用Ajax Get方法在前端获取MongoDB数据的主要内容,如果未能解决你的问题,请参考以下文章

前端与后端有哪几种ajax交互方法

Ajax 使用ajax加上get和post方法,通过后台加载数据,并在网页上进行显示案例+解释

前端获取后台数据的方法:ajaxaxiosfetch

jQuery – AJAX get() 和 post() 方法

用jQuery的ajax获取接口数据

ajax 文件下载