gpt4 book ai didi

javascript - 如何将 res 从一个函数传递到另一个函数?

转载 作者:行者123 更新时间:2023-11-30 15:44:22 25 4
gpt4 key购买 nike

FB.api('4', function (res) {
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
console.log(res.id);
console.log(res.name);
});

// viewed at http://localhost:8080
app.get('/test', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));

});

我创建了这个返回一些数据的小型 FB.api 调用。现在我想在访问 localhost:8080/test 时显示这些数据,我该怎么做?做那样的事情的方法是什么?有人可以指出一些可能的文档吗?

最佳答案

首先你在这样的快速路由中添加你的 FB.api 调用

app.get('/something', function(req, res) {
FB.api('4', function (result) {
if(!res || res.error) {
return res.send(500, 'error');
}

res.send(result);
});
});

然后在 index.html 中你可以添加一些 javascript 到你的页面并创建一个 XHR 来调用 'localhost:8080/something' 就像那样

index.html

<script type="text/javascript">
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://localhost:8080/something');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var data = xhr.responseText;

//Do whatever you want with this data.
}
}
xhr.send();
</script>

关于javascript - 如何将 res 从一个函数传递到另一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40321847/

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