how do you get the response back into the instance of the object?

Posted by randomdev on Stack Overflow See other posts from Stack Overflow or by randomdev
Published on 2010-01-15T21:16:01Z Indexed on 2010/05/09 0:08 UTC
Read the original article Hit count: 182

Filed under:

If you've written a class in JavaScript that calls a remote service's API, and that remote API offers a callback, how do you get the response back into the instance of the object that made the request?

I'll try to give you a very basic example FOO for making cross domain calls to BAR service which offers a callback. Please ignore the usual security concerns, (I own both servers).

function FOO() {

   this.response = null;
   this.execute = function(url) {

      var script = document.createElement('script');
  script.src = url;
  document.getElementsByTagName('head')[0].appendChild(script); 
   }
   this.catch = function(response) {
        this.response = response;
   }

}


var sample = new FOO();

sample.execute('http://barservices.com/sample/?callback={ plshelphere: this.catch}');

I have a way to make this work, but I'm curious if there is an "accepted approach" here.

Anyone have thoughts for me?

© Stack Overflow or respective owner

Related posts about JavaScript