Pass arguments into ajax onreadystatechange callback?

后端 未结 3 1950
渐次进展
渐次进展 2020-12-01 10:27

What\'s the normal pure javascript (i.e. not JQuery) way to pass arguments into an anonymous onreadystatechange callback?

For example:

function doReq         


        
3条回答
  •  一整个雨季
    2020-12-01 11:01

    Never too late! :-)

    Instead of passing data using arguments in xhttp.onreadystatechange which is somewhat complicated, one can just add properties to the xhr object itself.

    For instance:

    var m = "Hello!";
    var xhttp = new XMLHttpRequest();
    xhttp.m = m;
    xhttp.onreadystatechange = function()
    {
        var x, m;
        x = this;
        m = x.m;
        if ((x.readyState == 4) && (x.status == 200))
        {
            alert(m);
        }
    };
    // ...
    

提交回复
热议问题