我需要从;“成功”;jQuery中的in.ajax函数
我有一个JsTree,它根据从AJAX调用接收的JSON数据填充。这里是AJAX调用我需要从;“成功”;jQuery中的in.ajax函数,jquery,Jquery,我有一个JsTree,它根据从AJAX调用接收的JSON数据填充。这里是AJAX调用 function sendQuery(){ $.ajax({ context: this, url: 'http://localhost:8080/testMain', type: 'GET', dataType: 'text', success: function(data) {
function sendQuery(){
$.ajax({
context: this,
url: 'http://localhost:8080/testMain',
type: 'GET',
dataType: 'text',
success: function(data) {
// ^^^^ Need for sendQuery() to return DATA
},
error: function (xhr, ajaxOptions, thrownError){
alert('Error xhr : ' + xhr.status);
alert('Error thrown error: ' + thrownError);
}
});
}
我知道这里有一个范围问题。在JavaScript中,变量是根据声明函数定义的。我只是不知道如何从sendQuery()返回,然后我将其用作另一个函数的参数,该函数将解析JSON,这是另一个函数的参数,用于树的阶段。有点沮丧的是,时钟系统中的这一部分没有像我在Java中习惯的那样简单。非常感谢你的帮助,如果能奏效,我一定会接受的。干杯
编辑#1:好的,根据答案,我相信如果我以这种方式更改代码,它将允许数据从.ajax函数中流出。还有一个问题是如何让它回到程序流程中
function sendQuery(){
$.ajax({
context: this,
url: 'http://localhost:8080/testMain',
type: 'GET',
dataType: 'text',
success: getJson,
error: function (xhr, ajaxOptions, thrownError){
alert('Error xhr : ' + xhr.status);
alert('Error thrown error: ' + thrownError);
}
});
}
function getJson(data){
alert("Transmission Success.");
alert(data);
var obj = $.parseJSON(data);
alert("Parsing JSON Success.");
var apples = obj.apples;
alert(apples);
return apples;
}
好的,那么现在我如何将APPLES变量放入调用链中,调用链将为树准备数据
我需要将APPLES变量提供给一个函数,该函数将处理数据
使用回调编辑#2:
我花了一秒钟的时间才想到回电话。这是我能用它做的
这是我的原始树代码,它调用一系列函数来执行不同的操作,但最终以树将接受的形式获取数据
$(function () {
$("#client_tree").jstree({
"json_data": {"data": attachTree(stageTreeData(getJson(sendQuery())))},
"plugins" : [ "themes", "json_data", "ui" ]
}).bind("select_node.jstree", function (e, data) {
var msg = data.rslt.obj.attr("id");
alert(msg);
});
});
我目前正在尝试通过Ajax获取数据,在sendQuery()方法中,然后从中返回数据等…]
我稍微修改了一下,现在我不调用sendQuery(),jQuery调用它
$(function (){
$.ajax({
context: this,
url: 'http://localhost:8080/testMain',
type: 'GET',
dataType: 'text',
success: loadTree,
error: function (xhr, ajaxOptions, thrownError){
alert('Error xhr : ' + xhr.status);
alert('Error thrown error: ' + thrownError);
}
});
});
也改变了我的树加载代码一点
function loadTree(data){
$("#client_tree").jstree({
"json_data": {"data": attachTree(stageTreeData(getJson(data)))},
"plugins" : [ "themes", "json_data", "ui" ]
}).bind("select_node.jstree", function (e, data) {
var msg = data.rslt.obj.attr("id");
alert(msg);
});
}
我没有错误,没有异常,树被填充
谢谢大家的帮助
编辑#3修复一些次要内容:
移动到不,你不需要 你需要的是使用这些数据 但是您不能从sendQuery返回它们,因为调用是异步的,这意味着数据只有在sendQuery返回后才可用 解决方案是为您的
sendQuery
函数提供一个回调函数,当数据可用时,该函数将执行您想要对数据执行的操作:
function sendQuery(callback){
...
success: callback,
...
}
...
sendQuery(function(data){
// do things with data
});
不,你不需要那个 你需要的是使用这些数据 但是您不能从sendQuery返回它们,因为调用是异步的,这意味着数据只有在sendQuery返回后才可用 解决方案是为您的
sendQuery
函数提供一个回调函数,当数据可用时,该函数将执行您想要对数据执行的操作:
function sendQuery(callback){
...
success: callback,
...
}
...
sendQuery(function(data){
// do things with data
});
您尝试执行的问题是ajax调用是异步的。在您收到服务器的响应之前,sendQuery函数将返回,控制流将继续 使用它的方法是使用回调。当服务器返回响应时,将调用传递给
success()
的函数。基本上,您需要该函数从中断的地方开始处理管道。您希望该函数将响应解析为json,“stage for the tree”等
您需要将调用此函数的函数分为调用前发生的事情,然后是调用返回后发生的“其他所有事情”。“其他一切”是您在成功回调中想要的
示例(对调用此函数的用户进行一些假设)
您尝试执行的问题是ajax调用是异步的。在您收到服务器的响应之前,sendQuery函数将返回,控制流将继续 使用它的方法是使用回调。当服务器返回响应时,将调用传递给
success()
的函数。基本上,您需要该函数从中断的地方开始处理管道。您希望该函数将响应解析为json,“stage for the tree”等
您需要将调用此函数的函数分为调用前发生的事情,然后是调用返回后发生的“其他所有事情”。“其他一切”是您在成功回调中想要的
示例(对调用此函数的用户进行一些假设)
您需要在ajaxsuccess中使用数据。可以使用此代码所示的其他函数
function sendQuery(){
$.ajax({
context: this,
url: 'http://localhost:8080/testMain',
type: 'GET',
dataType: 'text',
success: loadTree,
error: function (xhr, ajaxOptions, thrownError){
alert('Error xhr : ' + xhr.status);
alert('Error thrown error: ' + thrownError);
}
});
}
function loadTree( data){
/* do something with data returned from ajax*/
}
您需要在ajaxsuccess中使用数据。可以使用此代码所示的其他函数
function sendQuery(){
$.ajax({
context: this,
url: 'http://localhost:8080/testMain',
type: 'GET',
dataType: 'text',
success: loadTree,
error: function (xhr, ajaxOptions, thrownError){
alert('Error xhr : ' + xhr.status);
alert('Error thrown error: ' + thrownError);
}
});
}
function loadTree( data){
/* do something with data returned from ajax*/
}
不要使用async:false。它已被弃用,会阻塞浏览器,而且总是一个非常糟糕的主意。不要使用async:false。它被弃用了,阻塞了浏览器,这一直是一个非常糟糕的主意。好吧,我实际上需要做的是调用sendQuery()并存储响应。然后调用另一个将返回该值的函数。对吗?您需要调用
sendQuery
,而sendQuery
将调用另一个函数。好的,那么我实际需要做的是调用sendQuery()并存储响应。然后调用另一个将返回该值的函数。对吗?您需要调用sendQuery
,而sendQuery
将调用另一个函数。哦,如果我传递一个函数,如success:functionName,它将把数据传递回函数。太棒了。非常感谢。我试试看。是的。success:functionName
的作用与success:function(data){functionName(data)}
确定刚才对问题进行了编辑,我需要将此数据发送到函数调用。有时间的时候请检查一下。感谢data
是对象var apples=data.apples代码>删除parseJSON,它已经完成了…假设您返回的是有效的JSON。可以在jsonlint.com中进行验证。好的,谢谢。那么,我现在如何调用getJson(),将数据返回到stageDataForTree(数据)函数。哦,如果我传递success:functionName这样的函数,它将把数据传递回函数。太棒了。非常感谢。生病的