0

I have the following code

function exec(param1,p2,fun){
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(fun){
        data=xmlhttp.responseText;
        fun(data);
    }
    // rest ajax connection code
}

when i call

 exec(param1,param2,
    function(data){
      alert (data);
    });

it says

Object not a function

in the definition at line fun('test');

any ideas ?

Rami Dabain
  • 4,709
  • 12
  • 62
  • 106

1 Answers1

1

You are overwriting fun with a new one in a narrower scope:

function exec(param1,p2,fun){
                        ^^^  - The function you pass

xmlhttp.onreadystatechange=function(fun){
                                    ^^^ - new variable (possibly an event object)

Change one of the variable names so you aren't masking it inside your callback function.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335