2

im pritty new in writing javascript with objects. And now im trying to do an ajaxcall and have a callback function, but i can reach the callback function within the ajax call.

This is the code, how shall i call the callback function?

function smarttalkObj() {

    this.base_url = 'http://';

    this.getPts = function() {

        $.ajax({
            url: this.base_url + 'getPTS',
            dataType: "jsonp",
            jsonp : "callback",
            jsonpCallback: "this.callback_pts",
            contentType: 'application/x-javascript'
        }); 
    }


    }

    this.callback_pts = function(data) {

        console.log(data);

    }


}

smarttalk = new smarttalkObj();

smarttalk.getPts();

2 Answers 2

3

The ajax method can receive a success and error callback functions as part of the settings. Something like this:

   $.ajax({
        url: this.base_url + 'getPTS',
        dataType: "jsonp",
        jsonp : "callback",
        contentType: 'application/x-javascript',
        success: function(data, textStatus, jqXHR){
            console.log(data);
            // your callback function here
        },
        error: function(jqXHR, textStatus, errorThrown){
            // callback in case of error
        }
    }); 
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the answer, but if you do want to call an outside function as callback and not use the "success" field. Any clue how to do that?
Without the success field it may be complicated. You can try setting the defaults in $.ajaxSetup({success: functionName}) and that would free up your original ajax call, but at the end it's the same thing. Why do you want to avoid the success parameter?
That was more for the structure of the file, find it easier to search and find functions more then nested functions. Thats why. Thank you very much alemangui for the answer.
1

Add success field:

success: function(data)
{
 // put code here
}

For your example:

$.ajax({
        url: this.base_url + 'getPTS',
        dataType: "jsonp",
        jsonp : "callback",
        jsonpCallback: "this.callback_pts",
        contentType: 'application/x-javascript',
        success: function(data)
                 {
                  // put code here
                  }
    }); 

You can use separate function too:

$.ajax({
        url: this.base_url + 'getPTS',
        dataType: "jsonp",
        jsonp : "callback",
        jsonpCallback: "this.callback_pts",
        contentType: 'application/x-javascript',
        success: getCallback
    }); 

  function getCallback (data)
  {
  // put code here
  }

1 Comment

Thank you for the answer, but if you do want to call an outside function as callback and not use the "success" field. Any clue how to do that with the structure of the object?

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.