1

I need to run the success of ajax from a method function of object, this work when a pass to normal variable function

methodOne = function(data) {
  alert(data);
};

$(document).on('click', ".clichere", function() {
  var callback = function(){};
  callback = methodOne;
  runAjax({dispatch_ajax : "testing"}, callback );
    });

function runAjax(data, succFunction) {

  $.ajax({
      type: "POST",
      data: data,
      success: succFunction,
      error: function(error) {
          alert(JSON.stringify(error));
      },
  });

} 

this not work

var myapp = function() {
   this.listTableOptions = function(data) {
    alert(data);
   };
};


$(document).on('click', ".clichere", function() {

  obj = new myapp();

  runAjax({dispatch_ajax : "testing"}, obj.listTableOptions() );

});

I can't not get the data in myapp object

1
  • you're calling the function too early - the second argument to runAjax should be obj.listTableOptions with no parentheses. Compare with your first, working example, where you pass callback in without invoking it. Commented Mar 14, 2021 at 17:06

1 Answer 1

2

You want to pass the function listTableOptions instead of executing it and passing the result so the following will work:

var myapp = function() {
   this.listTableOptions = function(data) {
    alert(data);
   };
};

$(document).on('click', ".clichere", function() {
  obj = new myapp();
  runAjax({dispatch_ajax : "testing"}, obj.listTableOptions );
});

Notice obj.listTableOptions instead of obj.listTableOptions()

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.