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');
        }
    });
});