views:

49

answers:

0

I have this functions

//send JSON-RPC request
var json_rpc = (function() {
    var id = 1;
    return function(url, method, params, success) {

        if (typeOf(params) != 'array') {
            params = [params];
        }
        var request = JSON.stringify({
                       'jsonrpc': '2.0',
                       'method': method, 
                       'params': params, 
                       'id': id++});
        return $.ajax({
            url: url, 
            data: request,
            success: success,
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                  error_msg('XHR error ' + XMLHttpRequest.status + ' ' +
                     XMLHttpRequest.responseText);
            },
            beforeSend: function(xhr) {
       console.log('before send');
       console.log(dir(xhr));
       xhr.onreadystatechange = function(){
          console.log('state');
      };
        },
            contentType: 'application/json',
            dataType: 'json',
            type:"POST"});
    }
})();

 

var rpc = function(method, success_callback) {
    //I use functional javascript library
    var fun = json_rpc.partial('rpc.php', method, _, function(data) {
        if (data['error']) {
            var e = 'Json-RPC (' + method + ') ' + data['error']['code'] + ": " + 
               data['error']['message'];
            error_msg(e);
        } else {
            info_msg("rpc sucess for method '" + method + "'");
            success_callback(data['result']);
        }
    });
    return function() {
        fun(Array.slice(arguments));
    };
};

and when I create function with rpc

var update_news = rpc('get_news', function(data) {
    if (data) {
       //update news
    }
});

and call it

$(document).ready(function() {
    ...
    update_news();
    ...
});

In Firefox everythig is fine, but in Opera and Chrome the function update_news is not executing, beforeSend is fired but onreadystatechange is not, but when I add

setTimeout(update_news, 0);

Then It's call normaly, also when I create synchronous call by putting async: false in $.ajax call or when I put timeout, timeout: 1. In click handlers it also run as expected.

$('#some_id').click(function() {
  update_news();
});

Anybody know why this is happening.