javascript - 检查响应后,发送第二个响应
问题描述
当我收到服务器对一个字符串的响应时,我想发送下一个 json 对象。我想检查第一个对象并相应地发送第二个对象。怎样才能在服务器上响应?
app.post('/demo', (request, response) => {
const data = {"name": "john", "surname": "doe"};
const check = "Schedulable";
if(check == "Schedulable"){
response.send(check); //I need to know if it is schedulable on client-side. Because i will use this information on client-side. How can i send string information. After that i want to send json object.
response.json(data);
}
else{
response.send("Not Schedulable");
}
});
Client-side Code:
{
const response = await fetch('/demo', options);
const info = await response.json();
//Normally i do this when i receive a single response. How can i do when i have double response.
}
解决方案
您可以发送一个包含两个变量的对象,而不是将它们作为您无法执行的单独部分发送。例如:
{ check: check, data: data }
或者你甚至可以使用速记然后去:
{ check, data }
我希望这有帮助。我自己对此很陌生。
推荐阅读
- reactjs - next.js mapStateToProps、mapDispatchToProps 和 getInitialProps
- python - Python 周数计算 - 现在已回答
- python - 在熊猫中使用元组作为索引键时,如何“通过传入类别参数来明确指定类别顺序”?
- macos - 允许 Macintosh 应用程序写入文件
- javascript - Sequelize bulkCreate 不包括新添加的列
- gnuplot - GNUPLOT:直方图聚集减少直方图间空间以增加框宽度
- c# - 如何使用 Identity with Entity Framework 与 AspNetUser 表建立一对一的关系
- ruby - Ruby RegExp - 匹配所有 CSS 选择器和指令
- ios - 非泛型类型不能与类型参数一起使用 (Realm Xamarin)
- html - 如何正确使用 flexbox 在 HTML/CSS 中实现 BorderLayout?