IE9中的JQuery.Form.JS XHR错误

IE9中的JQuery.Form.JS XHR错误,jquery,forms,internet-explorer-9,Jquery,Forms,Internet Explorer 9,我在IE9中使用jQuery.form.js时遇到了一个问题,其中XHR返回的是的DOM,而不是来自帖子的响应 我已经创建了一个示例来演示这个问题,它是基于IE9中的工作原理 <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> <script src="http://malsup.gith

我在IE9中使用jQuery.form.js时遇到了一个问题,其中XHR返回的是的DOM,而不是来自帖子的响应

我已经创建了一个示例来演示这个问题,它是基于IE9中的工作原理

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
    <script src="http://malsup.github.com/jquery.form.js"></script>
</head>
<body>
    <form action="http://jquery.malsup.com/form/file-echo2.php" method="post" enctype="multipart/form-data">
        <input type="file" name="myfile">
        <input type="submit" value="Submit">
    </form>     
    <div id="status"></div>
    <script>
        (function() {
            var status = $('#status');
            $('form').ajaxForm({
                //xhrFields: {withCredentials: true},
                beforeSend: function() {
                    status.html("Submitting...");
                },
                success: function() {
                    status.html("Done...");
                },
                complete: function(xhr) {
                    status.html(xhr.responseText);
                }
            }); 
        })();       
    </script>
</body>

鉴于最初的示例在IE9中工作,我认为我的JSFIDLE应该可以工作,但我就是想不出来。非常感谢您的帮助。

经过一夜的睡眠,头脑清醒了,我现在可以回答我自己的问题了

首先,我通过添加以下内容打开了调试:

$.fn.ajaxSubmit.debug = true;
通过调试,我可以在控制台中看到以下内容

[jquery.form] fileAPI :false
[jquery.form] state = loading
[jquery.form] cannot get iframe.contentWindow document: TypeError: Permission denied
[jquery.form] cannot get iframe.contentDocument: Error: Access is denied.
因此,问题是由CORS造成的,其中POST请求的响应加载到临时请求中,但父级无法访问

据我所知,在IE9中使用jquery.form.js插件无法执行CORS请求

但是,如果您在同一台服务器上编写自己的服务宿主,关键是按照官方文档中的解释,以HTML或XML形式返回响应

下面是我如何使用C Web API 2实现的代码片段:

public class UploadController : ApiController
{
    [HttpPost]
    public async Task<HttpResponseMessage> UploadFile()
    {
        //
        // Verify that this is an HTML Form file upload request
        //
        if (!Request.Content.IsMimeMultipartContent("form-data"))
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
        }

        var filesReadToProvider = await Request.Content.ReadAsMultipartAsync();

        //
        // Read the file... return a string (i.e. strResp)
        // 

        var resp = new HttpResponseMessage(HttpStatusCode.OK);
        resp.Content = resp.Content = new StringContent(strResp, Encoding.UTF8, "text/plain");

        return resp;
    }
}
然后在前端UI上,xhr.responseText现在应该是正确的,并作为字符串返回。使用JavaScript将字符串解析回JSON或数组中


希望这对其他面临类似问题的人有用。

对于具有多个文件上载控件的表单,我对IE11和jQuery.Form 3.51也有同样的问题。通过使用较旧版本的jQuery.Form,我了解了text/plain或text/html内容类型的要求,所以我已经准备好了。就是我打电话来的

drupal_add_http_header('Content-Type', 'text/html');
在我的处理函数中。几年前我在某个地方读到过,设置头应该是处理函数中的第一件事,所以它是函数中的第一行。无论如何,当我在调用之前添加一些调试代码时,它开始工作了!所以基本上我的解决方案是确保上面的函数调用不是我函数的第一行。不知道这是否合乎逻辑