如何获得此函数以返回使用jQuery.ajax检索的值? [英] How can I get this function to return value retrieved using jQuery.ajax?
问题描述
我需要返回动态加载的内容.我以为这是这样做的方法,但是函数返回空白.为从jQuery.ajax
检索到的html代码设置htmlCode
,我需要做什么?
I need to return dynamic loaded content. I thought this was the way to do it, but the function returns blank. What do I need to do in order to set htmlCode
with the html code retrieved from jQuery.ajax
?
// Get directory listing
function getHTML(instance, current_path, dir) {
var htmlCode = '';
jQuery.ajax({
type: "POST",
url: "../wp-content/plugins/wp-filebrowser/jquery.php",
dataType: 'html',
data: {instance: instance, current_path: current_path, dir: dir},
success: function(html){
htmlCode = html;
},
error: function(e) {
htmlCode = '[Error] ' + e;
}
});
return htmlCode;
}
推荐答案
之所以发生这种情况,是因为ajax请求需要一些时间来获取html,并且在html准备就绪之前会触发return语句. Javascript代码执行不会等待您的html返回.您实际上可以通过删除退货并放置两个警报来查看此情况.在成功事件中放入一个alert
,在您的return语句中放入一个alert
.第二个alert
会在之前发出警报.因此,即使您的html被获取,它也不会真正成功地返回到调用函数,因为在html准备就绪时已经触发了return语句.
This is happening because the ajax request takes some time to get the html and your return statement fires before the html is ready. Javascript code execution does not wait for your html to return. You can actually see this by removing the return and putting two alerts. Put one alert
inside the success event and one where you have put your return statement. The second alert
would alert before. So, even though your html is fetched, it is never actually returned successfully to the calling function because the return statement already fired by the time html is ready.
如果严格希望函数getHtml()
返回(实际上是call back
)html作为输出,则可以使用callback
,否则,可以使用Nick建议的方法.
You can use a callback
if you strictly want the function getHtml()
to return (well actually call back
) the html as an output, or else you can use the way suggested by Nick.
以下是使用回调的方法:-
Here is how to use a callback:-
function getHTML(instance, current_path, dir,callback)
{
var htmlCode = '';
jQuery.ajax({
type: "POST",
url: "../wp-content/plugins/wp-filebrowser/jquery.php",
dataType: 'html',
data: {instance: instance, current_path: current_path, dir: dir},
success: function(html){
callback(html); //instead of a return
},
error: function(e) {
htmlCode = '[Error] ' + e;
}
});
}
这样调用函数-
getHTML(instance, current_path, dir,
function(html)
{
//Write code to use the html here - this will run when the getHTML() function does callback with the output html
}
);
请注意函数定义getHTML(instance,current_path,dir,callback)中的callback
参数以及被调用函数中的相应function(html){}
部分.
Note the callback
parameter in the function definition getHTML(instance, current_path, dir,callback) and the corresponding function(html){}
part in the called function.
这样,您实际上定义了:-
This way, you actually define:-
- 输出准备就绪后,
call back
调用方函数的调用函数 - 和调用方函数在收到
call back
时执行某些操作.
- the called function to
call back
the caller function when the output is ready - and the caller function to do something when it receives the
call back
.
这篇关于如何获得此函数以返回使用jQuery.ajax检索的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!