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 ?

4
  • 5
    Works for me... jsfiddle.net/jHbsW Commented Aug 7, 2012 at 18:49
  • What does console.log(fun) (or console.log(exec)) show? Commented Aug 7, 2012 at 18:50
  • am sorry guys , its related to ajax onreadystatechange :s i updated the sample code Commented Aug 7, 2012 at 18:56
  • stackoverflow.com/questions/5373278/… Commented Aug 7, 2012 at 19:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.