[英]Showing data from Ajax response
I am trying to display my ajax response in a div, but I have no idea how to do that. 我正在尝试在div中显示我的Ajax响应,但是我不知道该怎么做。
My code is the following: 我的代码如下:
<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>
If you click on .tribe-mini-calendar-day-link
, the console says the following: 如果单击.tribe-mini-calendar-day-link
,控制台将显示以下内容:
`{status: true, html: "<p><strong>Pratice: </strong> 2017-09-23 till 2017-09-24</p>"}`
Now my question is, how do I display that code in the divright
? 现在我的问题是,如何在divright
显示该代码?
Your success function should be : 您的成功职能应该是:
success: function (response) {
if(response !== undefined && response.html !== undefined) {
$(".divright").html(response.html);
}
},
In success
section of AJAX request, assuming you are parsing response
right way. 在AJAX请求的success
部分中,假设您正在正确地解析response
。
var data = response.html;
if(data !== undefined)
$(".divright").html(data);
else
$(".divright").html("Error!");
Inside your success function you need to append html to the div. 在您的成功函数中,您需要将html附加到div。 This can be done very easily using jquery as 使用jquery可以很容易地做到这一点
if(response !== undefined && response.html !== undefined){
$( ".divright" ).html(response.html);
}
else{
$( ".divright" ).html("<div>Some Error Encountered</div>");
}
You should use Jquery .html response to update data in your div tag . 您应该使用Jquery .html响应来更新div标签中的数据。 such as $("#your_DIV_ID").html( data);
例如$("#your_DIV_ID").html( data);
Here is your your complete code. 这是您的完整代码。 ` `
<div id="my_Div_ID" 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) {
$("#my_Div_ID").html(response);
console.log(response)
},
error: function(request, error) {
alert("FOUT:" + error);
}
})
})
</script>
` `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.