如何在ajax请求中发送附加数据以及formData

时间:2014-01-29 11:14:13

标签: 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。

3 个答案:

答案 0 :(得分:2)

通过网址传递,

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

答案 1 :(得分:0)

试试这个,

    data:{'id':id,
          'formdata':formData,
},

答案 2 :(得分:0)

您不会两次发送数据。发送此格式:

data: {
  formData : formData,
  id:id
},