【发布时间】:2015-06-29 21:46:50
【问题描述】:
在我的前端 JavaScript 应用程序中,我发出一个 ajax 请求以从服务器获取数据。获得数据后,我想将那条信息返回给视图。
var view_data;
$.ajax({
url:"/getDataFromServer.json",
//async: false,
type: "POST",
dataType: "json",
success:function(response_data_json) {
view_data = response_data_json.view_data;
console.log(view_data); //Shows the correct piece of information
return view_data; // Does not work. Returns empty data
}
});
// return view_data; --> Keeping the return outside of the ajax call works but then I need to make my ajax call synchronous in order for this return clause to be executed after the ajax call fetches data.
我该怎么做?
【问题讨论】:
-
您应该将 jquery 标签添加到您的帖子中..
-
你在分配
view_data = response_data_json.view_data;那你为什么不使用view_data你为什么要在成功方法中返回它。 -
我通过将数据写入 cookie 然后在另一个函数中使用它来解决这个问题。
标签: javascript jquery ajax