简体   繁体   English

在ajax中使用参数调用onreadystatechange回调函数

[英]Calling onreadystatechange callback function with parameter in ajax

I found a question on this in SOF ..but didnt find the solution.. 我在SOF中找到了一个与此有关的问题,但没有找到解决方案。

this is my code.. 这是我的代码。

 if (mefofftasksxmlhttp!=null)
        {
            mefofftasksxmlhttp.onreadystatechange=sasi_ready(taskId);
            mefofftasksxmlhttp.open("GET",url,true);
            mefofftasksxmlhttp.send(null);
        }

callback function 回调函数

function sasi_ready(tskId)
        {
            if (mefofftasksxmlhttp.readyState==4)
            {
               if (mefofftasksxmlhttp.status==200)
                {
                    alert('Task Moved to completed Tasks');
                    $('#'+tskId).hide();
                }
                else
                {
                    alert("Problem retrieving XML data");
                }
            }
        }

this code is doesnt allowing me into the callback when i pass a parameter in the callback function ..but when i remove parameter..thats working fine.. 当我在回调函数中传递参数时,此代码不允许我进入回调,但是当我删除参数时,该函数正常工作。

where i went wrong? 我哪里出问题了?

As @Beetroot-Beetroot already said: You need to pass a function reference to onreadystatechange. 正如@ Beetroot-Beetroot已经说过的:您需要将函数引用传递给onreadystatechange。 If you want to pass a parameter, you could still call a function with this parameter as long as this function will return a function reference 如果要传递参数,则只要此函数将返回函数引用,仍可以使用此参数调用函数

function sasi_ready(tskId) {
   return function() {
        if (mefofftasksxmlhttp.readyState==4)
        {
           if (mefofftasksxmlhttp.status==200)
            {
                alert('Task Moved to completed Tasks');
                $('#'+tskId).hide();
            }
            else
            {
                alert("Problem retrieving XML data");
            }
        }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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