AJAX not working in IE in a WordPress Plugin

I am building a WordPress plugin and I am attempting to use AJAX. I am having trouble getting AJAX to work in IE.

The following attempts all work in FF/Chrome but fail in IE.

Read More

PHP:

add_action('wp_ajax_nonpriv_trying_ajax', 'trying_ajax');
add_action('wp_ajax_trying_ajax', 'trying_ajax');
function trying_ajax(){

    $response = json_encode( array( 'success' => true ) );

    header( "Content-Type: application/json" );

    echo $response;

    exit;
}

Javascript:

var ajaxData = {
    action: 'trying_ajax',
    state:  filterStates
    };


JQuery.post(myconvio_convio_functions.ajaxurl, ajaxData,
    function(data) {
        alert(data);
    },
    'json'
);

FF/Chrome returns a JSON object; however IE returns a 0.

http://codex.wordpress.org/AJAX_in_Plugins
says…

Error Return Values

If the AJAX request fails when the request url is
wp-admin/admin-ajax.php, it will return either -1 or 0 depending on
the reason it failed. Additionally, if an AJAX request succeeds, it
will return a 0.

I have tried this to account for caching by:

var d = new Date();
var n = d.getTime();
JQuery.post(myconvio_convio_functions.ajaxurl+"?x="+n, ajaxData,

I have tried a relative url to account for cross domain issues:

JQuery.post('/FWW/wp-admin/admin-ajax.php', ajaxData,

All the same results:
FF/Chrome returns a JSON object; however IE returns a 0.

Thanks for any help.

…. Changed to $.ajax and added success,error and complete – results are in the comments.
Still the same results, IE comes back as success but with response of 0

$.ajax({
url: '/FWW/wp-admin/admin-ajax.php',
type: 'POST',
data: ajaxData,
dataType: 'json',
success: function(response, status, xhr) {
    console.log(response);                                      
        // Firebug log: Object { success=true }

    alert('success - response: ' + response);                   
        // FF Alert: success - response: [object Object]
        // IE Alert: success - response: 0

    alert('success - status: ' + status);                       
        // IE&FF Alert: success - status: success

    alert('success - xhr.readyState: ' + xhr.readyState);
        // IE&FF Alert: success - xhr.readyState: 4

    alert('success - xhr.responseText: ' + xhr.responseText);
        // FF Alert: success - xhr.responseText: {"success":true}
        // IE Alert: success - xhr.responseText: 0

    alert('success - xhr.status: ' + xhr.status);
        // IE&FF Alert: success - xhr.status: 200

    alert('success - xhr.statusText: ' + xhr.statusText);
        // IE&FF Alert: success - xhr.statusText: OK
},
// error handler function never gets called on IE or FF
error: function(jqXHR, textStatus, errorThrown) {
    alert('error jqXHR: ' + jqXHR);
    alert('error textStatus: ' + textStatus);
    alert('error errorThrown: ' + errorThrown);
},
complete: function(jqXHR, textStatus){
    alert('complete - jqXHR.readyState: ' + jqXHR.readyState);
        // IE&FF Alert: complete - jqXHR.readyState: 4
    alert('complete - jqXHR.responseText: ' + jqXHR.responseText);
        // FF Alert: complete - jqXHR.responseText: {"success":true}
        // IE Alert: complete - jqXHR.responseText: 0

    alert('complete - jqXHR.status: ' + jqXHR.status);
        // IE&FF Alert: complete - jqXHR.status: 200

    alert('complete - jqXHR.statusText: ' + jqXHR.statusText);
        // IE&FF Alert: complete - jqXHR.statusText: OK

    alert('complete - textStatus: ' + textStatus);
        // IE&FF Alert: complete - textStatus: success

    }
});

Related posts

Leave a Reply