gpt4 book ai didi

javascript 在继续之前等待返回

转载 作者:行者123 更新时间:2023-11-30 13:35:39 25 4
gpt4 key购买 nike

我的 javascript 没有等待调用返回时出现问题。我现在已经知道javascript是异步的所以我想知道如何让这个方法调用等待结果。我无法控制前两个截图,因为它们是由用户上传的。我可以使用 jquery 或纯 javascript。提前致谢!

我有这个 javascript 调用

var value = somemethod("cmi.location");
/ /This is not getting set since it does not wait. alerts 'undefined'
alert(value);

有些方法看起来像下面的代码,

function somemethod(element){
var result;
result = API1.GetValue(element);
return result;
}

API 是通过执行以下代码实例化的窗口对象。从现在开始,我可以访问代码片段。

var API1 = new API();

API 是 javascript 中的对象,如下所示:

function API(){

};
API.prototype.GetValue=API_GetValue;

function API_GetValue(parameter){
$.ajax({
type:"POST",
async:false,
url:"method.do",
dataType:"xml",
data: {action: 'getValue', parameter: parameter, value: ''},
success:function(data){
//I am getting 0 here
return $(data).find('search').text();
}
});
}

最佳答案

function API_GetValue(parameter){
var newdata;
$.ajax({
type:"POST",
async:false,
url:"method.do",
dataType:"xml",
data: {action: 'getValue', parameter: parameter, value: ''},
success:function(data){
//I am getting 0 here
newdata = $(data).find('search').text();
}
});
return newdata;
}

你也可以这样做:

function API_GetValue(parameter){
var newdata = $.ajax({
type:"POST",
async:false,
url:"method.do",
dataType:"xml",
data: {action: 'getValue', parameter: parameter, value: ''}
}).responseText;
return $(newdata).find('search').text();
}

关于javascript 在继续之前等待返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5004580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com