如何在生成响应后,用另一种方法从ajax请求中获取响应

How to get the response from the ajax request in another method after the response has been generated?

本文关键字:响应 方法 ajax 请求 获取 另一种      更新时间:2023-09-26

我想要从发送请求的方法生成响应,我需要在另一个方法中生成该响应。请参阅以下代码以获取进一步的参考。

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",
             data : JSON.stringify(data),
             success : function(response) {
             },
             error : function(e) {  
                 alert("Object" +e);
                }  
               });  
   };

成功后产生的响应,我需要在下面的方法中得到。我写了下面的代码,但无法得到响应。请帮帮我。

function getValues(){
var response = foodLog();
console.log(response)
  }

您可以使用回调函数来高效地执行此操作。

function foodLog(callback){  //sending callback function as 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",
         data : JSON.stringify(data),
         success : callback, //calling callback function when success
         error : function(e) {  
             alert("Object" +e);
            }  
           });  
}

//callback function to handle response
function callback(response){
     console.log(response);
}

当您想调用函数"foodLog"时,

foodLog(callback);

注意:在调用此函数之前,必须确保已加载函数回调。你可能必须使用

$(document).ready(function(){
    foodLog(callback);
});

也许这是你想要的

success : function(response) {  
                 if (response == true){  
                     window.location = "/fitbase/foodlog/create/";   
                     getValues(response);  
                }  
         }
function getValues(response){
  console.log(response);
}
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",
         data : JSON.stringify(data),
         success : function(response) {
               getValues(response);
         },
         error : function(e) {  
             alert("Object" +e);
            }  
      });  
};

以这种方式使用变量。

var foodLogResponse = '';
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",
             data : JSON.stringify(data),
             success : function(response) {
                foodLogResponse = response;
             },
             error : function(e) {  
                 alert("Object" +e);
                }  
               });  
   };

否则,您可以从成功处理程序中调用getValues函数。

success : function(response) {  
                 if (response == true){    
                     getValues(response);  
                }  
         }