jQuery:在ajax调用成功后返回数据
jQuery:在ajax调用成功后返回数据
这个问题已经有答案了:
我有这样一个东西,它是对一个脚本的简单调用,这个脚本会给我返回一个值,一个字符串。
function testAjax() { $.ajax({ url: "getvalue.php", success: function(data) { return data; } }); }
但如果我像这样调用
var output = testAjax(svar); // output will be undefined...
那么我该如何返回这个值呢?下面的代码也似乎不起作用...
function testAjax() { $.ajax({ url: "getvalue.php", success: function(data) { } }); return data; }
admin 更改状态以发布 2023年5月20日
要从函数返回数据的唯一方法是进行同步调用而不是异步调用,但这会在等待响应时冻结浏览器。
您可以传递一个处理结果的回调函数:
function testAjax(handleData) { $.ajax({ url:"getvalue.php", success:function(data) { handleData(data); } }); }
像这样调用它:
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.