我有一段代码:
$.getJSON('http://myjsonurl', function(json){console.log(JSON.stringify(json.columns)); });这将在控制台中返回我从json响应中需要的所有内容。我的目标是将这个值转换成一个函数,这样我就可以在另一个地方调用它。(例如:
"columns" : getColumns();所以我做了一个这样的功能:
function getColumns() {
$.getJSON('http://myjsonurl', function(json){return JSON.stringify(json.columns); });
}
console.log(getColumns()); // and then call the function in the console log expecting to see the same result as before.但我所得到的只是不确定的。为什么?
更新:
这就是我如何实现我想要的。下面的代码将基于带有数据和列的json响应重新启动datatable (一些数据和列本身不支持)。代码将使用新的查询参数重新加载表,并包括按钮插件:
var theurl;
theurl = "http://myjson.json";
function updateQueryStringParameternondt(key, value) {
var table = $('#datatable-buttons').DataTable();
var ajaxurl = theurl;
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = ajaxurl.indexOf('?') !== -1 ? "&" : "?";
if (ajaxurl.match(re)) {
console.log( ajaxurl.replace(re, '$1' + key + "=" + value + '$2'));
theurl = ajaxurl.replace(re, '$1' + key + "=" + value + '$2');
table.destroy();
TableManageButtons.init();
}
else {
console.log( ajaxurl + separator + key + "=" + value);
theurl = ajaxurl + separator + key + "=" + value ;
table.destroy();
TableManageButtons.init();
}
}
TableManageButtons.init();
var handleDataTableButtons = function() {
0 !== $("#datatable-buttons").length &&
$.ajax( {
url:theurl,
dataType: 'json',
success: function ( json ) {
$("#datatable-buttons").DataTable({
"data" : json.data,
"columns": json.columns,
dom: "Bfrtip",
buttons: [{
extend: "copy",
className: "btn-sm"
}, {
extend: "csv",
className: "btn-sm"
}, {
extend: "excel",
className: "btn-sm"
}, {
extend: "pdf",
className: "btn-sm"
}, {
extend: "print",
className: "btn-sm"
}],
responsive: !0
});
}
} );
},
TableManageButtons = function() {
"use strict";
return {
init: function() {
handleDataTableButtons();
}
};
}();发布于 2016-05-07 09:10:31
@guest271314答案是正确的,应该指导您解决有关从getColumns方法返回未定义的问题。
我只想在这里指出一些关键的事情。
首先研究一下我刚才创建的扑通。正如您所看到的,这里的所有内容都是为了操作延迟对象(更多信息请参见这里 )。粗体解释说,延迟对象可以注册回调,如果您喜欢多个回调,则将它们链接起来,通过调用它们可以广播它们的状态以及它们的响应。它基于承诺设计,因此这样的方法返回一个可以被解析、同步或异步的承诺(大多数情况下,承诺在异步操作中是有用的)。
jQuery的异步方法,如$.ajax,返回一个承诺。$.getJSON没有什么不同,因为它最终调用了$.ajax,正如前面提到的,它返回了jQuery延迟的承诺。
有关jQuery的animation方法,请参见下面的@guest271314注释。
更多关于承诺这里的报道。
从文件
当绑定到集合的所有特定类型的动作,无论是否排队,都已完成时,返回诺言对象以进行观察。
promise,处理时要么是解决的,要么是拒绝的。resolve代表成功响应,reject代表失败。从文档中我们可以看到,Deferred对象有一些方法可以处理成功、失败或两者兼而有之。
deferred.done()添加要在解析延迟对象时调用的处理程序。 当延迟对象被拒绝时,deferred.fail()添加要调用的处理程序。 当延迟对象被解析或拒绝时,deferred.always()添加要调用的处理程序。
那么,让我们回到OP的问题。从代码中可以看到:
function getColumns() {
$.getJSON('http://myjsonurl', function(json){return JSON.stringify(json.columns); });
}您不可能知道getColumns状态,因为您不返回promise。在使用$.getJSON处理程序时,您实质上是在那里处理响应,而不发出诺言对象。当然,getColumns函数返回undefined,因为没有return的符号,默认情况下您会得到它。
对于deferred.then()方法,您还可以处理一个承诺,处理它的状态和它的进度。在我上面贴出的示例代码中,我不关心进度,只关心状态,所以在第一个示例中,承诺是用.then()方法处理的,第一个函数是成功处理程序,第二个函数是失败处理程序。从他们那里返回响应本质上意味着承诺得到解决。我也会回报你的承诺。
在注释掉部分中,您可以看到,如果希望并在always方法中解析响应,则只能返回承诺
发布于 2016-05-07 07:53:59
正如@MiguelBolan所指出的,getColmuns()不返回任何值。return $.getJSON() from getColumns();使用.then()访问异步函数返回的值
function getColumns() {
return $.getJSON("http://myjsonurl");
}
getColumns().then(function(json) {
console.log(JSON.stringify(json.columns))
}
// handle errors
, function err(jqxhr, textStatus, errorThrown) {
console.log(textStatus, errorThrown)
}; 发布于 2016-05-07 07:48:56
您的getColumns()函数需要返回变量,目前它只是返回给函数,而不是out。试试这个:{
function getColumns() {
var ret;
$.getJSON('http://myjsonurl', function(json){
ret = JSON.stringify(json.columns);
});
return ret;
}https://stackoverflow.com/questions/37085922
复制相似问题