jQuery ajax在IE中返回数据为空

时间:2022-10-08 08:48:11

I'm using ajax to get some data then based on the data use html() to put it on the page.

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

In IE, the data returned is empty (it's html). It still triggers the success function, but the html is not there. I've set the dataType: "text" but still doesn't work.

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

Any ideas?

有任何想法吗?

Thanks!

谢谢!

EDIT:

编辑:

Here's the exact code:

这是确切的代码:

$('#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);
                    }
            }
        });
})

It's submitting a zip code. On submit, it checks to make sure it's a number and 5 digits in length. If that passes, it goes to the ajax and checks to make sure it's a valid zip code (database check), if that fails it returns 'false' as text, if it's good then it returns some html.

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

It works in Firefox and Chrome, but not in IE. When it's good, it submits the form but the data returned alerts empty and is appended as empty space.

它适用于Firefox和Chrome,但不适用于IE。如果它很好,它会提交表单,但返回的数据警告为空并附加为空格。

4 个解决方案

#1


6  

your code don't work simply because it is buggy, it have nothing to do with IE. it should be something like below.

你的代码不能简单地工作,因为它有bug,它与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.
  • 注意:chrome和firefox不显示错误这一事实并不意味着错误不存在; Internet-explorer只是在每次运行格式错误的代码时都会显示错误。另外,我强烈怀疑你的代码是否真的像预期的那样工作,因为它不像你想象的那么好。我想问题出在你的代码中,与jQuery本身无关,或者它是ajax函数或dataType。

#2


3  

One thing could be that this URL is cached by the browser. If you obtain a 304 status your response's text it will be empty. Check your HTTP cache headers, and adjust them properly. You could also trying to use POST (only GET are cached). Also, have a look to Content-Length if is properly set.

有一件事可能是浏览器缓存了这个URL。如果您获得304状态,您的回复文本将为空。检查HTTP缓存标头,并正确调整它们。您也可以尝试使用POST(仅缓存GET)。另外,如果设置正确,请查看Content-Length。

If nothing of above works, inspect the network's call will give to you (and us) some additional details, try the IE's dev tools.

如果上述任何内容都无效,请检查网络的呼叫是否会给你(和我们)一些额外的细节,试试IE的开发工具。

You could also try to have the same request using XMLHttpRequest's object directly (assuming you're using IE>6).

您也可以尝试直接使用XMLHttpRequest对象获取相同的请求(假设您使用的是IE> 6)。

#3


1  

I have same problem in IE and Google Chrome

我在IE和谷歌浏览器中遇到同样的问题

$.ajax is not working in this browser because of security reason.

由于安全原因,$ .ajax无法在此浏览器中运行。

so if you want to run explicitely

所以,如果你想明确地运行

for chrome run chrome with

用于铬润滑铬

chrome.exe --disable-web-security

and in IE you need to change setting from Menu>Tools>internet Options

在IE中,您需要从菜单>工具>互联网选项更改设置

#4


1  

Some versions of IE will not ~repair~ invalid HTML in XHR requests and simply discard it; I've been bitten by this before.

某些版本的IE不会在XHR请求中修复无效的HTML并简单地丢弃它;我以前一直被这个咬过。

Make sure the HTML returned is valid for the doctype IE thinks you're using (use the developer tools in IE to see), and check your HTML with a validator tool. Obviously, it will complain about missing <html>, <head>, etc, but ignores those and focus on the other errors. once you've fixed those, it should start working.

确保返回的HTML对IE认为您正在使用的doctype有效(使用IE中的开发人员工具查看),并使用验证工具检查您的HTML。显然,它会抱怨缺少,等,但忽略了那些并关注其他错误。一旦你修好了它,就应该开始工作了。

Also, aSeptik's answer is full of good tips to improve your code.

此外,aSeptik的答案充满了改进代码的好建议。

#1


6  

your code don't work simply because it is buggy, it have nothing to do with IE. it should be something like below.

你的代码不能简单地工作,因为它有bug,它与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.
  • 注意:chrome和firefox不显示错误这一事实并不意味着错误不存在; Internet-explorer只是在每次运行格式错误的代码时都会显示错误。另外,我强烈怀疑你的代码是否真的像预期的那样工作,因为它不像你想象的那么好。我想问题出在你的代码中,与jQuery本身无关,或者它是ajax函数或dataType。

#2


3  

One thing could be that this URL is cached by the browser. If you obtain a 304 status your response's text it will be empty. Check your HTTP cache headers, and adjust them properly. You could also trying to use POST (only GET are cached). Also, have a look to Content-Length if is properly set.

有一件事可能是浏览器缓存了这个URL。如果您获得304状态,您的回复文本将为空。检查HTTP缓存标头,并正确调整它们。您也可以尝试使用POST(仅缓存GET)。另外,如果设置正确,请查看Content-Length。

If nothing of above works, inspect the network's call will give to you (and us) some additional details, try the IE's dev tools.

如果上述任何内容都无效,请检查网络的呼叫是否会给你(和我们)一些额外的细节,试试IE的开发工具。

You could also try to have the same request using XMLHttpRequest's object directly (assuming you're using IE>6).

您也可以尝试直接使用XMLHttpRequest对象获取相同的请求(假设您使用的是IE> 6)。

#3


1  

I have same problem in IE and Google Chrome

我在IE和谷歌浏览器中遇到同样的问题

$.ajax is not working in this browser because of security reason.

由于安全原因,$ .ajax无法在此浏览器中运行。

so if you want to run explicitely

所以,如果你想明确地运行

for chrome run chrome with

用于铬润滑铬

chrome.exe --disable-web-security

and in IE you need to change setting from Menu>Tools>internet Options

在IE中,您需要从菜单>工具>互联网选项更改设置

#4


1  

Some versions of IE will not ~repair~ invalid HTML in XHR requests and simply discard it; I've been bitten by this before.

某些版本的IE不会在XHR请求中修复无效的HTML并简单地丢弃它;我以前一直被这个咬过。

Make sure the HTML returned is valid for the doctype IE thinks you're using (use the developer tools in IE to see), and check your HTML with a validator tool. Obviously, it will complain about missing <html>, <head>, etc, but ignores those and focus on the other errors. once you've fixed those, it should start working.

确保返回的HTML对IE认为您正在使用的doctype有效(使用IE中的开发人员工具查看),并使用验证工具检查您的HTML。显然,它会抱怨缺少,等,但忽略了那些并关注其他错误。一旦你修好了它,就应该开始工作了。

Also, aSeptik's answer is full of good tips to improve your code.

此外,aSeptik的答案充满了改进代码的好建议。