我对javascript很陌生,我整天都在寻找解决这个问题的方法。我发现了很多问题/答案,但没有任何效果。
我有一个包含文件上传的页面,我想通过服务器端验证例程运行此文件,然后在页面上显示验证结果。除了 IE(Safari、Chrome 和 Chromium、Firefox)之外,我在所有方面都表现出色。
为了使文件上传在 IE 上工作,用户需要通过提交按钮提交表单。因此,我使用 HTML 中的条件注释将其添加到页面上。在 javascript 中,我使用在这里找到的 IE 检测功能。这部分工作正常。
这是发出ajax请求的函数:
function handleUpload()
{
    showSpinner();
    var dFile = new FormData(document.getElementById("devConsValidate")); // this works in IE but only if called by a submit method 
    $.ajax(
    {
        cache: false,
        async: false,
        url: 'validator.php',
        type: 'POST',
        data: dFile,
        dataType: 'JSON',
        processData: false, // this is key for formdata
        contentType: false, // so is this
        success: function(response)
        {
            var result = response.result;
            var validationCode = response.validation_code;
            var errors = response.errors;
            var htmlCode = '<h2>';
            if (result == 'SUCCESS')
                htmlCode += '<font color="green">';
            else
            {
                htmlCode += '<font color="red">';
            }
            htmlCode += 'Validation ' + result + '</font></h3>';
            if (result == "FAIL")
            {
                htmlCode += '</center>';
                for (var i in errors)
                    htmlCode += errors[i] + '<br>';
                htmlCode += '<br><a href="validator_help.php">Validator Help    </a>';
            }
            else
            {
                htmlCode += 'Please use the following code when submitting your request.<br><br><font color ="#00aa54" size="+2">' + validationCode + '</font>';
            }
            // make some beautiful html
            $('#resultsArea').html(htmlCode);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) 
        {
            alert('error: ' + errorThrown);
        }
    }); 
}
在文件更改事件或正在提交的表单上调用函数 handleUpload()。validator.php 返回一个 json_encoded 值数组。
问题是在使用 IE 时,ajax 成功回调永远不会触发,浏览器只会在空白页面上显示原始 JSON 字符串,因此 ajax 调用正在工作并返回预期数据。在其他浏览器上,成功回调触发并解析 json 并将适当的代码插入到页面上的 div 中。
完整的 javascript 文件: http: //pastebin.com/G9MYBis5
有任何想法吗?