我正在使用节点获取模块。如何将响应存储在变量中,以便从以下发布请求中进一步使用?
node-fetch git repo在这里。
var url = 'http://127.0.0.1:10010/login';
var data = {
email : 'rajan10@gmail.com',
password : 'sha256md5'
};
fetch(url, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(res => res.json());
答案 0 :(得分:1)
好吧,then(res => res.json());
是响应。如果要存储它,只需在返回它之前将其分配给某个变量即可。
var r;
...
.then(res => {
r = res;
res.json();
});
然后您可以根据需要使用r
。