jquery Ajax response “undefined” with Internet Exp

2019-02-24 01:07发布

I've got a situation with jquery ajax requests.

<script type="text/javascript">
$(document).ready(function() {
    $.ajax({
        type: "POST",
        async: false,
        cache: false, 
        url: "/ajax/script.php",
        data: { display: 'user', user_id: '1'}
    }).done(function( msg ) {
        if (msg !== "") {
            alert(msg);
        }
    });
});
</script>

With Chrome & Firefox I've html code generate by the script /ajax/script.php With IE8 (I've not tried with 6, 7 and 9+) I have Undefined

Do someone knows how to fix that?

Edit: I'm using jquery 1.7.2

3条回答
贼婆χ
2楼-- · 2019-02-24 01:41

Returning the MIMEType of application/json; charset=utf8 caused this same behavior for me in IE8. Changing it to application/json; made IE8 magically start functioning.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-02-24 01:46

Well, since you don't define "msg" it is undefined. done() expects a function, multiple functionsor nothing as arguments.

If you want a callback from you ajax call, you should use

    $.ajax({
        type: "POST",
        async: false,
        cache: false, 
        dataType: 'text',
        url: "/ajax/script.php",
        data: { display: 'user', user_id: '1'},
        success: function(data) {
            // do something
        }

    });

Edit: I'm using jquery 1.7.2 if it can help

查看更多
女痞
4楼-- · 2019-02-24 01:51

I solved the problem changing code from:

response.setCharacterEncoding("UTF8");

to:

response.setCharacterEncoding("UTF-8");
查看更多
登录 后发表回答