Question

I'm trying to display the response back from an ajax call using jquery within a pop up. My problem is that I don't know how to correctly make use of the callback method, shown below.

Many thanks

 var data = "my data to post";

 .loadAjaxWindow(url, jr.dialog(data), true,
        function(xhr, textStatus, errorThrown) {
            // stuff
        }, 
        true, 'post', data);

The method looks like the following:

 // Ajax Method

 loadAjaxWindow: function (url, callback, async, errorCallback, cache, type, data) {
      success: function (data, textStatus, xhr) {
         callback(data);
      },
 }

I've tried the following although the alert is not show?

 var myCallBack = function(obj) {
       alert("");
 };

 .ajax.loadJson(url, myCallBack,
     true,
     function(xhr, textStatus, errorThrown) {
        // stuff
     }, 
     true, 
     'post', 
     JSON.stringify(data)
 );
Was it helpful?

Solution

callback here, is a function which is called when the success callback of ajax is called...

so as an example..

 var callback=function(obj){
      console.log(obj);
       jr.dialog(obj) //jr ??
      //do yourstuff with ajax returned data which is as obj here
 };

loadAjaxWindow: function (url, callback, async, errorCallback, cache, type, data) {
  success: function (data, textStatus, xhr) {
     callback(data);
  },
}

so this will log datas return by the ajax call if you check in your console.

OTHER TIPS

Your callback syntax is wrong, it should be

.loadAjaxWindow(url, jr.dialog(data), true, function(data) {
            // use data to do further processing, it is the value returned by server
        }, true, 'post', data);

where data is the value returned by the server, you can use it to do whatever you want to do

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top