开发者

execute a function when the success data arrived in jquery ajax

开发者 https://www.devze.com 2023-01-29 18:26 出处:网络
I know that\'s some kind of callback concept, but have no idea how to do that. $.ajax({ \'url\': \'/test/\',

I know that's some kind of callback concept, but have no idea how to do that.

$.ajax({
       'url': '/test/',
       'type': 'POST',
       'data': {'age': age},
       'dataType': 'html',
       'success': function(data, textStatus, xhr) {

         //I want when the data arrives, then execute another 开发者_Go百科function, because the function is too big to place here.

        }

});


If you need to execute only some other function with takes the data as a parameter, do this:

$.ajax({
   'url': '/test/',
   'type': 'POST',
   'data': {'age': age},
   'dataType': 'html',
   'success': myFunction
});

//then, defined anywhere that's in scope:
function myFunction(data) {
  //do something with data
}

If you need to do some work then call that function...do just that:

$.ajax({
   'url': '/test/',
   'type': 'POST',
   'data': {'age': age},
   'dataType': 'html',
   'success': function(data) {
     //do stuff...
     myFunction(data);
   }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消