Php 使用Yii2和Jquery在模态对话框中重新加载PJAX容器
我在自定义Php 使用Yii2和Jquery在模态对话框中重新加载PJAX容器,php,ajax,gridview,yii2,pjax,Php,Ajax,Gridview,Yii2,Pjax,我在自定义modal中重新加载PJAX容器时遇到了一些问题,就像bootstrap的modal一样 这段代码的目标是重新加载pjax容器,以重新加载pjax的Listview中所有提交的注释 初始重新加载由jQuery执行,但随后在jQuery.pjax.js中引发异常: jquery.pjax.js:740 Uncaught TypeError: Cannot read property '0' of null 它是名为extractContainer的函数的一部分: if (fullD
modal
中重新加载PJAX
容器时遇到了一些问题,就像bootstrap的modal
一样
这段代码的目标是重新加载pjax容器,以重新加载pjax
的Listview
中所有提交的注释
初始重新加载由jQuery
执行,但随后在jQuery.pjax.js
中引发异常:
jquery.pjax.js:740 Uncaught TypeError: Cannot read property '0' of null
它是名为extractContainer的函数的一部分:
if (fullDocument) {
var $head = $(parseHTML(data.match(/<head[^>]*>([\s\S.]*)<\/head>/i)[0]))
var $body = $(parseHTML(data.match(/<body[^>]*>([\s\S.]*)<\/body>/i)[0])) <-- error
} else {
var $head = $body = $(parseHTML(data))
}
if(完整文档){
var$head=$(解析HTML(data.match(/]*>([\s\s.]*)/i)[0]))
var$body=$(parseHTML(data.match(/]*>([\s\s.]*)/i)[0]))多亏了@Beowulfenator,我才走上了解决此问题的正确道路
显然,pjax
模块返回了modal
/frontend/index
后面的实际页面的URL,而不是/frontend/post/detail?id=id
。将modal
的pjax
路由到正确的URL相当简单:
$('#submit-comment').on('click',function(){
event.preventDefault();
var user_id = '<?=Yii::$app->user->identity->id?>';
var content = $('#comment-content').val();
var post_id = '<?=$model->id?>';
var associative_id = $(this).data('assoc-id');
$.post('/frontend/post/submit-comment', {
user_id : user_id,
post_id : post_id,
content : content,
associative_id: associative_id
}, function(response){
if(response['response'] == true){
$('#comment-content').removeClass('error-form');
$('#comment-content').addClass('success-form');
$(this).data('assoc-id','null');
$('#comment-content').val('');
$.pjax.reload(
{
container:"#pjax-post-comments",
url: '/frontend/post/detail?id=<?=$model->id?>', //manually added the url
timeout: 5000
}
);
}
else{
alert(response['errors']);
$('#comment-content').addClass('error-form');
$('#comment-content').removeClass('succes-form');
}
});
});
检查服务器响应,它是否包含该内容?很抱歉延迟回复。您的评论为我指出了正确的方向。pjax接收的URL与modal的URL不匹配。有关更多信息,请参阅我的回答。
$('#submit-comment').on('click',function(){
event.preventDefault();
var user_id = '<?=Yii::$app->user->identity->id?>';
var content = $('#comment-content').val();
var post_id = '<?=$model->id?>';
var associative_id = $(this).data('assoc-id');
$.post('/frontend/post/submit-comment', {
user_id : user_id,
post_id : post_id,
content : content,
associative_id: associative_id
}, function(response){
if(response['response'] == true){
$('#comment-content').removeClass('error-form');
$('#comment-content').addClass('success-form');
$(this).data('assoc-id','null');
$('#comment-content').val('');
$.pjax.reload({container:"#pjax-post-comments",timeout: 5000});
}
else{
alert(response['errors']);
$('#comment-content').addClass('error-form');
$('#comment-content').removeClass('succes-form');
}
});
});
$('#submit-comment').on('click',function(){
event.preventDefault();
var user_id = '<?=Yii::$app->user->identity->id?>';
var content = $('#comment-content').val();
var post_id = '<?=$model->id?>';
var associative_id = $(this).data('assoc-id');
$.post('/frontend/post/submit-comment', {
user_id : user_id,
post_id : post_id,
content : content,
associative_id: associative_id
}, function(response){
if(response['response'] == true){
$('#comment-content').removeClass('error-form');
$('#comment-content').addClass('success-form');
$(this).data('assoc-id','null');
$('#comment-content').val('');
$.pjax.reload(
{
container:"#pjax-post-comments",
url: '/frontend/post/detail?id=<?=$model->id?>', //manually added the url
timeout: 5000
}
);
}
else{
alert(response['errors']);
$('#comment-content').addClass('error-form');
$('#comment-content').removeClass('succes-form');
}
});
});
$('#submit-comment').on('click',function(){
event.preventDefault();
event.stopImmediatePropagation();
$(this).unbind('click');
alert('');
var user_id = '<?=Yii::$app->user->identity->id?>';
var content = $('#comment-content').val();
var post_id = '<?=$model->id?>';
var associative_id = $(this).data('assoc-id');
$.post('/frontend/post/submit-comment', {
user_id : user_id,
post_id : post_id,
content : content,
associative_id: associative_id
}, function(response){
if(response['response'] == true){
$('#comment-content').removeClass('error-form');
$('#comment-content').addClass('success-form');
$(this).data('assoc-id','null');
$('#comment-content').val('');
$.pjax.reload({container:"#pjax-post-comments",url: '/frontend/post/detail?id=<?=$model->id?>',replace:false,timeout: 5000});
init();
}
else{
alert(response['errors']);
$('#comment-content').addClass('error-form');
$('#comment-content').removeClass('succes-form');
}
});
});