显示来自Ajax响应的数据

时间:2017-09-19 09:40:12

标签: javascript jquery ajax

我试图在div中显示我的ajax响应,但我不知道该怎么做。

我的代码如下:

<div class="divright">
   Div Content
</div>


< script >
  $(document).on("click", '.tribe-mini-calendar-day-link', function() {
    var date = $(this).data('day');
    $.ajax({

      type: "POST",
      url: TribeMiniCalendar.ajaxurl,
      data: {
        action: 'event_details',
        date: date
      },
      success: function(response) {
        console.log(response)
      },
      error: function(request, error) {
        alert("FOUT:" + error);
      }
    })
  })
</script>

如果单击.tribe-mini-calendar-day-link,控制台会显示以下内容:

`{status: true, html: "<p><strong>Pratice: </strong> 2017-09-23 till 2017-09-24</p>"}`

现在我的问题是,如何在divright

中显示该代码

4 个答案:

答案 0 :(得分:3)

您的成功功能应该是:

success: function (response) {
   if(response !== undefined && response.html !== undefined) {
      $(".divright").html(response.html);
   }
},

答案 1 :(得分:2)

在AJAX请求的success部分中,假设您正确解析response

var data = response.html;
if(data !== undefined)
    $(".divright").html(data);
else
    $(".divright").html("Error!");

答案 2 :(得分:1)

在您的成功功能中,您需要将html附加到div。使用jquery作为

可以很容易地完成
if(response !== undefined && response.html !== undefined){
    $( ".divright" ).html(response.html);
}
else{
    $( ".divright" ).html("<div>Some Error Encountered</div>");
}

答案 3 :(得分:0)

您应该使用Jquery .html响应更新div标签中的数据。  例如gid

这是您的完整代码。 `

$("#your_DIV_ID").html( data);

`

相关问题