使用Ajax提交带有文件的Symfony 2嵌入式表单

用户名

我正在Symfony 2中创建具有文件验证功能的嵌入式文件上传表单。对于文件上传,我使用了以下示例http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html;对于嵌入式表单,则使用了此http:// symfony.com/doc/current/cookbook/form/form_collections.html这是完美的工作,但是我必须使用ajax提交表单,我该怎么办?

以下是我如何使用ajax提交表单的示例。

$("#submit_form").click(function() {

        var $form = $(this).parents('form:first');
        var $that = $(this);
        $.ajax({
            type: 'POST',
            dataType: 'json',
            cache: false,
            data: $form.serialize(),
            success: function($data) {
                if ($data.status == 'ok') {
                    $that.parents('#lightbox').html($data.template);
                }
            },
            url: $form.attr('action')
        });

        return false;
    });

所以问题是我无法使用ajax传递文件。

加格里鲁特

使用此插件http://malsup.com/jquery/form/奇迹般有效!

这可以提交表单,也可以上传文件。您需要做的就是调用ajaxSubmit,然后实现处理响应的方法。

$('#myForm2').submit(function() { 
    $(this).ajaxSubmit({success: showResponse}); 
}); 

function showResponse(responseText, statusText, xhr, $form)  { 
    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        '\n\nThe output div should have already been updated with the responseText.'); 
} 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章