如何在生成响应后,用另一种方法从ajax请求中获取响应
How to get the response from the ajax request in another method after the response has been generated?
我想要从发送请求的方法生成响应,我需要在另一个方法中生成该响应。请参阅以下代码以获取进一步的参考。
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);
}
}
相关文章:
- Ember.js:将Em.$.getJSON转换为promise并将响应绑定到控制器上下文的正确方法
- jQuery/Ajax-试图通过Ajax创建POST方法并获得对HTML的响应
- 画布如何在paperJs中使用pathData方法做出响应
- 从javaspring方法管理http.get响应
- 有没有任何方法可以从javascript进行跨域请求,并访问它可能生成的错误响应类型(401404等)
- jQuery$.ajax方法.成功:使用响应数据的函数
- 无法调用方法'发送'express js中未定义的(响应未定义)
- 试图理解返回javascript方法的服务器响应
- 创建响应图像的最简单方法是什么
- 制作预制响应功能的最佳方法是什么
- 无法解析javascript中的json字符串,这是java方法的响应
- 跨域 Post 方法 ajax 调用使用 jQuery 和 XML 响应
- 检查 JSON 响应是空还是空的最佳方法
- 是否存在任何使用 sass/指南针进行响应式菜单的方法
- Tumblr API 标记方法 - URL 没有相应的响应
- 在 ajax 响应中调用应用程序配置方法 - AngularJS
- 重写Node.js Express响应方法
- Sailsjs:基于请求参数设置响应方法
- 服务器和客户端之间的响应方法错误
- ASP.NET WebMethod以json形式返回对象,但不在响应方法中