ajax multipart/form-data post 字符集 utf-8

ajax multipart/form-data post charset utf-8

我有一个包含文件上传和需要 UTF-8 编码的文本输入的表单。当我尝试通过 ajax 发送表单时,我无法将 contentType 更改为 false,那么我该如何保护我的文本!?

    $.ajax({
            url         : 'url';?>',
            type: "POST",             
            data: new FormData(this), 
            contentType: false,       
            cache: false,            
            processData:false,        
            success: function(data)   
            {      
                 alert 'ok';
            }

您可以在表单提交中使用此代码:

$("#formId").submit(function(){

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

    $.ajax({
        url: 'your_url',
        type: 'POST',
        data: formData,
        async: false,
        success: function (data) {
            alert(data)
        },
        cache: false,
        contentType: false,
        processData: false
    });

    return false;
});

嗯,我完全没听懂,每个 ajax post 都是 utf-8。检查 docs

If the data is a Document, it is serialized before being sent. When sending a Document, versions of Firefox prior to version 3 always sends the request using UTF-8 encoding; Firefox 3 properly sends the document using the encoding specified by body.xmlEncoding, or UTF-8 if no encoding is specified.