以下答案引用自GPT-3大模型,请合理使用:
实例
此处我们可以通过在请求参数中传一个关键字来标识。比如在请求参数中新增一个type,type=1表示搜索医院信息,type=2表示搜索院区信息。
前端请求代码:
```js
fetch('/search', {
method: 'POST',
body: JSON.stringify({
type: 1,
keyword: 'xxx'
})
})
.then(res => res.json())
.then(data => {
// 处理响应数据
});
后端处理代码:
// 搜索入口
app.post('/search', (req, res) => {
const { type, keyword } = req.body;
let data = [];
if (type === 1) {
// 搜索医院信息
data = searchHospital(keyword);
} else if (type === 2) {
// 搜索院区信息
data = searchRegion(keyword);
}
// 响应
res.json({
code: 200,
data
});
});
// 搜索医院信息函数
const searchHospital = (keyword) => {
return [{...}]
}
// 搜索院区信息函数
const searchRegion = (keyword) => {
return [{...}]
}
```
如果我的回答解决了您的问题,请采纳我的回答
请求参数 也一样 那说明 所有 用到它的 页面 只要 搜索 返回数据都一样,不太 合理吧 。 应该是 接口一样,每个页面参数 不一样 还查不多