简体   繁体   中英

IE8 & 9 - no jquery ajax response object

jquery version: http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js

    $.ajax({
        url: CI_ROOT + current_page + '/get_results' ,
        type: 'post',
        data: { 'primary_key': primary_key, 'search_category':search_category, 'sub_category': sub_category ,'search_page':search_page, 'search_order': search_order, 'project_type': project_type},
        beforeSend: function(){
            $('.browse-list').html(spinner);
            $('.page-number-nav').html('');
        },          
        success: function(r){
            var response_obj = jQuery.parseJSON(r.responseText);

            $('.browse-list').html('');

            if (response_obj.status == 'SUCCESS')
            {               
                $('#sort_menu').hide();
                $('#sort_type').hide();

                if ((response_obj.results != 'No results') && (search_category != 'group') && (search_category == 'title' || (primary_key > 0)))
                {
                    $('#sort_menu').show(); 
                    $('#sort_type').show();             
                }

                $('.browse-list').html(response_obj.results);
                $('.page-number-nav').html(response_obj.pagination);
            }
        }
    }); 

if (r.responseText == undefined){alert('Empty');} returns Empty for IE 8 & 9, but works for IE10 & all other browsers

I've read & tried every post here I can find that might be relevant, cannot understand why this only bombs for those two browsers, and what to check for

TIA!

Try giving you ajax call a dataType like so:

dataType: 'json'

so it knows what kind of data you are expecting. You should also return the content type in the response header from the server as application/json - your json_encode method just converts your object to JSON, but apparently does not apply the correct headers (ref php.net )

You'll want to add:

header('Content-Type: application/json;charset=utf-8');

With the dataType declared explicitly there shouldn't be any need for the jQuery.ParseJSON , as it will convert it before it calls the success handler, so the parameter r will be the JSON you are expecting :)

Also just a note (as OP found out) that the header function call needs to be before your json_encode call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM