javascript - How to get the response from the ajax request in another method after the response has been generated? -
javascript - How to get the response from the ajax request in another method after the response has been generated? -
i want response generated method request has been sent , need generated response in method. please, refer below code farther reference.
function foodlog(){ var data={ servings : $('#demo_vertical').val(), calories : $('#calories').text(), carbs : $('#carbs').text(), }; $.ajax({ type : "post", contenttype: "application/json; charset=utf-8", url : "/fitbase/foodlog/create", datatype: "json", info : json.stringify(data), success : function(response) { }, error : function(e) { alert("object" +e); } }); };
the response generated after success need in below method. wrote below code not able response. please help me out.
function getvalues(){ var response = foodlog(); console.log(response) }
you may utilize callback function efficiently.
function foodlog(callback){ //sending callback function input var data={ servings : $('#demo_vertical').val(), calories : $('#calories').text(), carbs : $('#carbs').text(), }; $.ajax({ type : "post", contenttype: "application/json; charset=utf-8", url : "/fitbase/foodlog/create", datatype: "json", info : json.stringify(data), success : callback, //calling callback function when success error : function(e) { alert("object" +e); } }); } //callback function handle response function callback(response){ console.log(response); }
when want phone call function "foodlog",
foodlog(callback);
note: have create sure function callback loaded before calling this. may have use,
$(document).ready(function(){ foodlog(callback); });
javascript ajax
Comments
Post a Comment