How to send FormData objects with Ajax

This question already has an answer here:

  • Sending multipart/formdata with jQuery.ajax 11 answers

  • I believe you could do it like this :

    var fd = new FormData();    
    fd.append( 'file', input.files[0] );
    
    $.ajax({
      url: 'http://example.com/script.php',
      data: fd,
      processData: false,
      contentType: false,
      type: 'POST',
      success: function(data){
        alert(data);
      }
    });
    

    Setting processData to false lets you prevent jQuery from automatically transforming the data into a query string. See the docs for more info.

    Setting the contentType to false is imperative, since otherwise jQuery will set it incorrectly.


    There are a few yet to be mentioned techniques available for you. Start with setting the contentType property in your ajax params.

    Building on pradeek's example:

    $('form').submit(function (e) {
        var data;
    
        data = new FormData();
        data.append('file', $('#file')[0].files[0]);
    
        $.ajax({
            url: 'http://hacheck.tel.fer.hr/xml.pl',
            data: data,
            processData: false,
            type: 'POST',
    
            // This will override the content type header, 
            // regardless of whether content is actually sent.
            // Defaults to 'application/x-www-form-urlencoded'
            contentType: 'multipart/form-data', 
    
            //Before 1.5.1 you had to do this:
            beforeSend: function (x) {
                if (x && x.overrideMimeType) {
                    x.overrideMimeType("multipart/form-data");
                }
            },
            // Now you should be able to do this:
            mimeType: 'multipart/form-data',    //Property added in 1.5.1
    
            success: function (data) {
                alert(data);
            }
        });
    
        e.preventDefault();
    });
    

    In some cases when forcing jQuery ajax to do non-expected things, the beforeSend event is a great place to do it. For a while people were using beforeSend to override the mimeType before that was added into jQuery in 1.5.1. You should be able to modify just about anything on the jqXHR object in the before send event.


    You can send the FormData object in ajax request using the following code,

    $("form#formElement").submit(function(){
        var formdata = new FormData($(this)[0]);
    });
    

    This is very similar to the accepted answer but an actual answer for the question topic. This will submit the form elements automatically in the FormData and you don't need to manually append the data to FormData variable.

    The ajax method looks like this,

    $("form#formElement").submit(function(){
        var formdata = new FormData($(this)[0]);
        //append some non-form data also
        formData.append('other_data',$("#someInputData").val());
        $.ajax({
            type: "POST",
            url: postDataUrl,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function(data, textStatus, jqXHR) {
               //process data
            },
            error: function(data, textStatus, jqXHR) {
               //process error msg
            },
    });
    

    You can also manually pass the form element inside the FormData object as parameter like this

    var formElem = $("#formId");
    var formdata = new FormData(form[0]);
    

    Hope it helps. ;)

    链接地址: http://www.djcxy.com/p/3304.html

    上一篇: jQuery上传进度和AJAX文件上传

    下一篇: 如何使用Ajax发送FormData对象