javascript - jQuery ajax 在 IE 中返回空数据

标签 javascript jquery

我正在使用 ajax 获取一些数据,然后根据这些数据使用 html() 将其放在页面上。

在 IE 中,返回的数据是空的(它是 html)。它仍然会触发成功功能,但 html 不存在。我已经设置了 dataType: "text"但仍然不起作用。

有什么想法吗?

谢谢!

编辑:

具体代码如下:

$('#frm').submit(function(event){
        event.preventDefault();
        var self = this;
        z = $('#zipcode');
        if(z.val() != '' && z.val().length == 5) {
            var value = z.val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
            var intRegex = /^\d+$/;
            if(!intRegex.test(value)) {
                return false;
            }
        } else {
            return false;
        }

        $.ajax({
            url: "/ajax/zip", 
            type: 'GET',
            async: false,
            dataType: 'html',
            data:   {zipcode: $('.zip_field').val()},
            success: function(data) {
                    if(data == 'false'){
                        error($(".zip_field"));
                        return false;
                    }else{
                        self.submit();
                        $('.container').empty().append(data);
                    }
            }
        });
})

它正在提交邮政编码。提交时,它会检查以确保它是一个数字和 5 位数字的长度。如果通过,它会转到 ajax 并检查以确保它是一个有效的邮政编码(数据库检查),如果失败,它将作为文本返回“false”,如果没有问题,则返回一些 html。

它适用于 Firefox 和 Chrome,但不适用于 IE。一切正常时,它会提交表单,但返回的数据会提示为空,并会附加为空白区域。

最佳答案

你的代码不能正常工作只是因为它有问题,它与 IE 无关。 它应该像下面这样。

$('#frm').submit(function (e) {
    e.preventDefault(); // this one already act as return false...

    var form = $(this);
    // why you have .zip_field & #zip_code ?
    var zip_code = $.trim($('#zip_code').val());
    var valid = (zip_code.length === 5 && !isNaN(zip_code)) ? true : false;

    if (valid) {
        $.get('/ajax/zip', {
            zipcode: zip_code
        }, function (data) {
            if (data == 'false') {
                alert('error!');
            } else {
                //if you submit the form here 
                //form.submit(); then the line below 
                //is totally useless
                $('.container').html(data);
                //.empty().append() is = .html()
            }
        });
    }
});
  • NOTE: the fact that chrome and firefox don't display errors dosn't mean that errors are not there; internet-explorer simply tend to display errors everytime it run through malformed code etc. Also i strongly doubt that your code work really as expected since it is not so good as you may think. I guess the problem is in your code and have nothing to do with jQuery itself or it's ajax function or dataType.

关于javascript - jQuery ajax 在 IE 中返回空数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7450442/

相关文章:

javascript - angular2如何区分组件 View 中的Light DOM或Shadow DOM

javascript - Bootstrap 3悬停时弹出的对话框

jQuery AJAX,json数据成为Django中的关键,无法解析数据

javascript - jQuery 在动画和颜色变化之间暂停

jquery - 如何使用 jQuery 从下拉数组中获取值

javascript - 使用 jquery 更新表

javascript - 获取相同的数据到两个选择元素

javascript - jquery转json的问题

javascript - jQuery 循环遍历背景图像

javascript - 如何使用 Typescript、React 和 Webpack 导入 SCSS 或 CSS 模块