MooTools - 在父函数中返回 onSuccess 变量
请帮助我! :(
如何仅使用 getValue() 返回字符串?我不知道如何将变量应用到父函数中。我很头疼:
<script>
function getValue() {
new Request.JSON({
data: JSON.encode({
"serviceName": "demoTest",
"methodName": "someValue",
"parameters": []
}),
onSuccess: function(data) { // data = a string with some text
var string = data;
console.log(string); // returns the string as expected
},
url: "gateway/?contentType=application/json"
}).send();
return string; // returns undefined
}
</script>
Help me please! :(
How can I return the string by just using getValue()? I don't know how to apply the variable into the parent function. I'm getting headache:
<script>
function getValue() {
new Request.JSON({
data: JSON.encode({
"serviceName": "demoTest",
"methodName": "someValue",
"parameters": []
}),
onSuccess: function(data) { // data = a string with some text
var string = data;
console.log(string); // returns the string as expected
},
url: "gateway/?contentType=application/json"
}).send();
return string; // returns undefined
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是
return
早在触发onSuccess
(回调)函数之前就被触发了。一种方法是同时使用回调函数。你想用的方法是行不通的。这是您的 getValue 函数:
这是一个回调函数:
最后在加载的文档上调用该函数:
The problem here is that the
return
is fired long before theonSuccess
(callback) function is triggered. One approach would be to use also a callback function. The way you want to do it won't work.Here is your getValue function:
Here is a callback function:
And finally call the function on document loaded: