javascript - 如何在ajax请求中与formData一起发送附加数据

标签 javascript jquery ajax symfony twig

我有一个文件输入值的以下ajax请求onChange

$(':file').change(function(){
        var file = this.files[0];

        var formData = new FormData($('form')[0]);

        var id= $(this).attr('data-post-id'); // What I want to send additionaly to file

        $.ajax({
            url: "http://localhost/bghitn/web/app_dev.php/image/upload",           
            type: 'POST',           
            xhr: function() { 
                var myXhr = $.ajaxSettings.xhr();

                if(myXhr.upload){ 
                    myXhr.upload.addEventListener('progress',progressHandlingFunction, false);
                }
                return myXhr;
            },
            success: completeHandler,
            data: formData,
            data:{id:id}, // what is actually not working
            cache: false,
            contentType: false,
            processData: false
        });        
    });

我正在向 html 标记添加一个属性,其中包含我希望与文件相关数据一起发送的 id

<input type="file" name="img" data-post-id="{{entity.id}}" />

我在 Symfony2 下使用 PHP,例如:

  if ($request->isMethod('POST')) {
            $image = $request->files->get('img');

}

我需要一种等效的方法来获取 id。

最佳答案

通过url传递,

$(':file').change(function(){
        var file = this.files[0];

        var formData = new FormData($('form')[0]);

        var id= $(this).attr('data-post-id'); // What I want to send additionnaly to file

        $.ajax({
            url: "http://localhost/bghitn/web/app_dev.php/image/upload?id="+id,
            //...........................................................^....

            type: 'POST',           
            xhr: function() { 
                var myXhr = $.ajaxSettings.xhr();

                if(myXhr.upload){ 
                    myXhr.upload.addEventListener('progress',progressHandlingFunction, false);
                }
                return myXhr;
            },
            success: completeHandler,
            data: formData,

            cache: false,
            contentType: false,
            processData: false
        });        
    });

或者您可以使用append方法添加id

$(':file').change(function(){
        var file = this.files[0];

        var formData = new FormData($('form')[0]);
        formData.append("id",id);
        //...............^.......
        var id= $(this).attr('data-post-id'); // What I want to send additionnaly to file

        $.ajax({
            url: "http://localhost/bghitn/web/app_dev.php/image/upload?",


            type: 'POST',           
            xhr: function() { 
                var myXhr = $.ajaxSettings.xhr();

                if(myXhr.upload){ 
                    myXhr.upload.addEventListener('progress',progressHandlingFunction, false);
                }
                return myXhr;
            },
            success: completeHandler,
            data: formData,

            cache: false,
            contentType: false,
            processData: false
        });        
    });

关于javascript - 如何在ajax请求中与formData一起发送附加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21429384/

相关文章:

jquery - 使用 Ajax 的 PUT 请求(处理 Spring)

jquery - 将 Fancybox 插件转换为 Angular 指令

javascript - 带有 formData 的 Ajax 不会在 asp.net Controller 中绑定(bind) child 的对象数组

javascript - 无法更改数组位置并以 Angular 动态显示内容

javascript - 如何在 ng-repeat Angular 中使用两个列表

MVC4 应用程序中的 Jquery UI 对话框从部分 View 加载对话框内容

jquery - 如何在 div 边框上创建不带颜色的不透明度渐变?

ajax - Yii、Selenium 和多个提交按钮

javascript - 扩展 jQuery 的 ajax 功能

javascript - 代理设置一个值