javascript - Passing parameters with JQuery Promises -


can provide nicer way below using jquery promises:

the problem want pass parameters alongside data returned ajax , modify data returned before posting again in request.

var obj= $(this);  $.get(   "fetch_data1",    {},   function(data){     callback1(data, obj)   });  function callback1(data, obj){   var anotherparam = "test"    $.get(     "fetch_data2",      {"data":data, "q":obj.text()},     function(data2){       callback2(data, obj, anotherparam)     }); }  function callback2(data2, obj, another_param){   obj.text(data2 + another_param); } 

jquery.get shorthand equivalent to:

jquery.ajax({   url: url,   data: data,   success: success,   datatype: datatype }); 

using full function, can supply context object, , use in callback function this:

jquery.ajax({   url: 'fetch_data1',   data: {},   context: { mycontext: $(this) },   success: callback1 });  function callback1(data) {   var context = this;   var whatever = context.mycontext.text(); }; 

your context object can elaborate need be, containing whatever data need pass callback function.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -