jQuery的:返回Ajax调用成功后数据 [英] jQuery: Return data after ajax call success
本文介绍了jQuery的:返回Ajax调用成功后数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的事情,它是一个脚本,一个简单的电话,让我回值,一个字符串。
函数testAjax(){
$阿贾克斯({
网址:getvalue.php
成功:功能(数据){
返回的数据;
}
});
}
但如果我叫这样的事情
无功输出= testAjax(SVAR); //输出将是不确定的...
让我怎么能返回值? 下面code似乎并不管用...
函数testAjax(){
$阿贾克斯({
网址:getvalue.php
成功:功能(数据){
}
});
返回的数据;
}
解决方案
从函数返回数据的唯一方法是使同步调用,而不是一个异步调用,但这样会冻结浏览器,而它的等待为响应
您可以通过在回调函数处理结果:
函数testAjax(handleData){
$阿贾克斯({
网址:getvalue.php
成功:功能(数据){
handleData(数据);
}
});
}
这样称呼它:
testAjax(函数(输出){
//在这里使用的输出
});
//注:通话将不会等待结果,
//因此它会继续与code,而在这里等待。
i have something like this, where it is a simple call to a script that gives me back a value, a string..
function testAjax() {
$.ajax({
url:"getvalue.php",
success:function(data) {
return data;
}
});
}
but if i call something like this
var output = testAjax(svar); // output will be undefined...
so how can i return the value? the below code does not seem to work either...
function testAjax() {
$.ajax({
url:"getvalue.php",
success:function(data) {
}
});
return data;
}
解决方案
The only way to return the data from the function would be to make a synchronous call instead of an asynchronous call, but that would freeze up the browser while it's waiting for the response.
You can pass in a callback function that handles the result:
function testAjax(handleData) {
$.ajax({
url:"getvalue.php",
success:function(data) {
handleData(data);
}
});
}
Call it like this:
testAjax(function(output){
// here you use the output
});
// Note: the call won't wait for the result,
// so it will continue with the code here while waiting.
这篇关于jQuery的:返回Ajax调用成功后数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文