[英]$.ajax POST is sending data, but it is showing as undefined in the response from the server
[英]Showing data from Ajax response
我正在尝试在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
显示该代码?
您的成功职能应该是:
success: function (response) {
if(response !== undefined && response.html !== undefined) {
$(".divright").html(response.html);
}
},
在AJAX请求的success
部分中,假设您正在正确地解析response
。
var data = response.html;
if(data !== undefined)
$(".divright").html(data);
else
$(".divright").html("Error!");
在您的成功函数中,您需要将html附加到div。 使用jquery可以很容易地做到这一点
if(response !== undefined && response.html !== undefined){
$( ".divright" ).html(response.html);
}
else{
$( ".divright" ).html("<div>Some Error Encountered</div>");
}
您应该使用Jquery .html响应来更新div标签中的数据。 例如$("#your_DIV_ID").html( data);
这是您的完整代码。 `
<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.