Treat callback response in $.ajax() as JSON

2019-08-13 13:13发布

问题:

I have:

<script>
    $('#email').on('blur', function(){
        email = $(tihs).val();
        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: {
                'email': email,
                'job': 'check',
            },
            dataType: "JSON",
            success: function (response) {
                // the response from PHP is smth like:
                // {"status":"failed","reason":"email_not_validated"}
                // now I want to:
                if(response.status == 'success'){

                }else{

                }
            }   
        })
    });
</script>

This seems to work on every browser except IE, why is that?

Am I doing the right thing? the only thing which I need is to access the returned data like response.status and response.reason .

Thanks for helping

回答1:

This is a mentioned IE10 bug that can be fixed by adding

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />

in the <head>. Note in a <head> there should not be another meta tags with X-UA-Compatible as the previous one will be overridden.