简体   繁体   English

使用formdata Internet Explorer 10上传Ajax文件

[英]Ajax file upload using formdata Internet Explorer 10

I've been looking for a solution but never succeeded. 我一直在寻找解决方案,但从未成功。 I have a ajax call with formdata for an image to upload to server. 我有一个带formdata的ajax调用,用于将图像上传到服务器。 The code looks like 代码看起来像

$.ajax({

    type:'POST',

    url:'url',

    data:UPLOAD_DATA,

    processData:false,

    contentType:false,

    cache:false,

    timeout:10000,

    mimeType:'multipart/form-data',

    dataType:'json',

    success:function(data){

        if(data.success){

            alert(data.success);

        }else if(data.redirect){

        window.location.redirect=data.redirect;

        }else{

            alert(JSON.stringify(data));

        }

    }

});

While sending the console remains at showing "pending". 在发送控制台时,仍然显示“待处理”。 This code works in all the browser except ie10 该代码在除ie10之外的所有浏览器中均有效

please help me.. 请帮我..

Previous IE version (8 and previous) were not accepting a JSON response to the ajax submit of a multipart form. 先前的IE版本(8及以前的版本)不接受对多部分表单的ajax提交的JSON响应。 Thus resulting in never going into the success callback, and in some situations suggesting the user to download the JSON response as a text file. 因此导致永远不会进入成功回调,并且在某些情况下建议用户将JSON响应下载为文本文件。

I suppose IE10 is not making the same mistake. 我想IE10不会犯同样的错误。 If it does, you might consider returning the JSON content as text, set request dataType to text, and parse the text response back to JSON client side in the success callback. 如果是这样,您可能会考虑以文本形式返回JSON内容,将request dataType设置为text,然后在成功回调中将文本响应解析回JSON客户端。

However, before searching for headaches, please have a look at jQuery Form plugin which will handle the ajax submit of a form (multipart or not) and provide callbacks like the usefull uploadProgress one. 但是,在寻找头痛之前,请先看一下jQuery Form插件 ,它将处理表单的Ajax提交(是否为多部分),并提供有用的uploadProgress之类的回调。

Iam sorry it was a silly mistake. 我很抱歉这是一个愚蠢的错误。 I think the url as relative path in the server doesnt support in internet explorer, by giving full https://" ... " path solved the problem. 我认为通过提供完整的https://“ ...”路径,可以将URL作为服务器中相对路径的url在Internet Explorer中不支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM