Grails将JSON作为“文本”发送,但IE加载其ajax的URL并显示其响应:为什么?

时间:2015-08-11 14:41:19

标签: javascript jquery json internet-explorer grails

我一直在开发一个应用程序,它必须在两种不同的情况下提交带有文件上载字段的表单。在这个相同的应用程序中,这两种情况都可以在Firefox和Chrome中完美运行,但是当我们在IE上运行它时,其中一种情况就会失败。

情况#1 (它在FF,Chrome,IE9-7中完美运行):

/* controller */
def salvarArquivo() {   
    /* service calls, etc */

    uploadStatusJSON = uploadStatus as JSON     
    render uploadStatusJSON.toString()
}

-

/* JS - I'm using ajaxForm plugin by [malsup][1] */
$('#formularioEnviarArquivo').ajaxForm({
    beforeSubmit: validateFileUploadForm,
    clearForm   : true,
    dataType    : 'text',
    success     : function(dataStr) {
        var dataObj = $.parseJSON(dataStr);
        if (dataObj.status == false) {
            /* do this */
        } else {
            /* do that */
        }
    }
});

它返回一个正确的JSON文本,它被解析为JS对象,JS代码正确处理它,我有一个很好的页面,有一个很好的用户体验

情况#2 (在IE9或更早版本中失败):

/* controller */
def salvarComentarioAjax(){
    /* service, queries, etc
    * ask me more details */    

    def historicoJSON = historico as JSON
    render historicoJSON.toString() 
} // end method

-

/* JS */
$(document).ready(function(){
    /* validation stuff */  

    $('#formEnviarHistorico').ajaxForm({
        beforeSubmit    : function(){
            $('#submitFile').button('loading');
        },
        clearForm       : true,
        dataType        : 'text',
        success         : function(dataStr){
            var data = $.parseJSON(dataStr);
            if(data.status == false){
                /* do this */
            }else{
                /* do that */
            }   
        } // end success
    });
});

-

enter image description here

{"status":true,"comentario":"teste","arquivos":[{"status":true,"nome":"Hydrangeas.jpg","caminho":"http://192.168.0.81:8080/vs3/arq/atendimentos/20150811111634517_224442_Hydrangeas.jpg","mensagem":"OK"}],"nomeUsuario":"CINTIA BERNARDI","confidencial":false,"dataCadastro":"2015-08-11T14:16:34Z","id":251769,"foto":{"img":{"FD":{"class":"java.io.FileDescriptor"},"channel":{"class":"sun.nio.ch.FileChannelImpl","open":true},"class":"java.io.FileInputStream"},"path":"images/img_perfil/4a94303760487537892d8af0b5c47642.jpg","nomeFoto":"4a94303760487537892d8af0b5c47642.jpg"}}

我可以验证什么?还要别的吗?我一直在阅读很多关于将contentType设置为'text/plain'等的建议,但可能我已经尝试了所有这些建议,将ajax的dataType设置为'text''json'

1 个答案:

答案 0 :(得分:2)

令人费解,但肯定的是,我的客户端表单验证powered by jQuery Validate Plugin正在导致它。

/* JS */
$(document).ready(function(){

    /* validation stuff -> ACTUALLY IT IS: */  
    $('form[name=formEnviarHistorico]').validate(validacaoHistorico);
    /* It is jQuery Validate plugin!!! */

    $('#formEnviarHistorico').ajaxForm({
        beforeSubmit    : function(){
            $('#submitFile').button('loading');
        },
        clearForm       : true,
        dataType        : 'text',
        success         : function(dataStr){
            var data = $.parseJSON(dataStr);
            if(data.status == false){
                /* do this */
            }else{
                /* do that */
            }   
        } // end success
    });
});

如果用户浏览器是IE9或更早版本并且运行良好,我刚创建了一个IF条件以避免此验证行。目前,我的验证仅在服务器端进行。