jquery Ajax response “undefined” with Internet Exp

2019-02-24 01:44发布

问题:

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

回答1:

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.



回答2:

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



回答3:

I solved the problem changing code from:

response.setCharacterEncoding("UTF8");

to:

response.setCharacterEncoding("UTF-8");