Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Symfony在html模式标记中的JSONResponse_Json_Ajax_Symfony_Rendering_Bootstrap Modal - Fatal编程技术网

使用Symfony在html模式标记中的JSONResponse

使用Symfony在html模式标记中的JSONResponse,json,ajax,symfony,rendering,bootstrap-modal,Json,Ajax,Symfony,Rendering,Bootstrap Modal,谢谢你抽出时间 我正在尝试加载一个表单,其中包含要在引导模式中编辑的数据。 我能够从我的控制器加载JsonResponse,但它显示原始JSON代码 我的控制器JsonResponse: //more code $response = new JsonResponse( array( 'message' => 'Success!', 'form' => $this->renderVi

谢谢你抽出时间

我正在尝试加载一个表单,其中包含要在引导模式中编辑的数据。 我能够从我的控制器加载JsonResponse,但它显示原始JSON代码

我的控制器JsonResponse:

    //more code
    $response = new JsonResponse(
                array(
            'message' => 'Success!',
            'form' => $this->renderView('EPAdminBundle:Admin:formmmm.html.twig', array(
                'event' => $event,
                'form' => $form->createView(),
            ))), 200);

      return $response;
我正试图在这个视图中渲染窗体

查看test.html.twig:

<a data-toggle="modal" href="{{ path('ep_admin_test_form', {'slug': 'fete-1'}) }}" data-target="#modalForm" id="test">Click me !</a>

<div class="modal fade" id="modalForm" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content" id="loadHere">
            Loading...
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $("#test").on("click", function () {
            url = $(this).attr('href');
            $.ajax({
                type: 'POST',
                url: url,
                dataType: 'json'
            }).done(function (jqXHR, textStatus, errorThrown) {
                if (typeof jqXHR.responseJSON !== 'undefined') {
                    if (jqXHR.responseJSON.hasOwnProperty('form')) {
                        $('#loadHere').html(jqXHR.responseJSON.form);
                    }
                    $('.form_error').html(jqXHR.responseJSON.message);
                } else {
                    alert(errorThrown);
                }
            });
        });
    });
</script>

加载。。。
test.html.twig中的脚本:

<a data-toggle="modal" href="{{ path('ep_admin_test_form', {'slug': 'fete-1'}) }}" data-target="#modalForm" id="test">Click me !</a>

<div class="modal fade" id="modalForm" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content" id="loadHere">
            Loading...
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $("#test").on("click", function () {
            url = $(this).attr('href');
            $.ajax({
                type: 'POST',
                url: url,
                dataType: 'json'
            }).done(function (jqXHR, textStatus, errorThrown) {
                if (typeof jqXHR.responseJSON !== 'undefined') {
                    if (jqXHR.responseJSON.hasOwnProperty('form')) {
                        $('#loadHere').html(jqXHR.responseJSON.form);
                    }
                    $('.form_error').html(jqXHR.responseJSON.message);
                } else {
                    alert(errorThrown);
                }
            });
        });
    });
</script>

$(函数(){
$(“#测试”)。在(“单击”,函数(){
url=$(this.attr('href');
$.ajax({
键入:“POST”,
url:url,
数据类型:“json”
}).done(函数(jqXHR、textStatus、errorshown){
if(jqXHR.responseJSON的类型!==“未定义”){
if(jqXHR.responseJSON.hasOwnProperty('form')){
$('#loadHere').html(jqXHR.responseJSON.form);
}
$('.form_error').html(jqXHR.responseJSON.message);
}否则{
警报(错误抛出);
}
});
});
});
加载为JSON的formmmm.html.twig:


&时代;
接近
l’évènement修改:

{{form_start(form,{'attr':{'class':'form horizontal col-lg-12'}}}} //更多代码
//使用剪下的,因为它无法显示代码
您必须按照教程进行操作:

如果我想加载脚本,那么脚本就被窃听了

几天后,通过使用控制台并使用下一种方法查看jqXHR对象,我自己找到了解决方案:

var objs = Object.getOwnPropertyNames(jqXHR);
for (var i in objs) {
      console.log(objs[i]);
 }
问题是jqXHR.responseJSON.hasOwnProperty('form')不存在

$('#loadHere').html(jqXHR.form);
if(jqXHR.form的类型!=“未定义”){
if(jqXHR.hasOwnProperty('form')){
$('#loadHere').html(jqXHR.responseJSON.form);
}
$('.form_error').html(jqXHR.responseJSON.message);
}否则{
警报(“错误”);

}
在调用render()之后,需要调用getContent()。使用@Liutas的解决方案这不是问题所在。在我的代码中,我使用->renderView,这相当于render()->getContent()。我做到了,它给了我相同的JSON答案。从外观上看,唯一的问题是表单。这是您无法获得结果HTML的部分。请享受它在我的模式中加载的更多类似内容:。谢谢你的回答。