javascript - 从开关盒中检索数据
问题描述
我从 Javascript 开始,我的任务是从 API 检索数据。
今天我是一个步骤,我必须根据用户的请求过滤我的数据。
在这里,我想检索 html 的值,然后将其连接到 JS 中的 switch case。然后根据用户选择的值获取API的URL。
为了让您理解,代码中存在的 URL 等于数据库,我想选择要在我的表中显示的数据库。
我不知道我是否让自己明白了。无论如何,我与您分享我的代码。我一直在我的大脑周围,我可能错过了一些东西。任何帮助将不胜感激。谢谢
function boutonSubmit(){
bounceType = document.getElementById('BouncesType').value;
datestart = document.getElementById('dateS').value;
dateend = document.getElementById('dateE').value;
//bounceCode = getElementById('#dateS').value;
var SelectDB = document.getElementById('selectdb').value;
switch(SelectDB){
case 'Desclopinette':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=867XXXXXXXXXXXvGN&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Chrysolum':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=utxyiwq1K8S04WauVIa0&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Acanthius':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=v6PjXXXXXXXXXXX8y682&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Bellapourpre':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=xj530XXXXXXXXXXXRIZlD&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Alibigratis':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=SwyXXXXXXXXXXXdKz7z&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Bullecreatif':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=CN7XXXXXXXXXXXw30llOD&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Cacologia':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=oxjs3XXXXXXXXXXXeGjg&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Elenaparc':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=t02ZrrXXXXXXXXXXXoBm1&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Histoiredepoint':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=juiHFXXXXXXXXXXXSY68y5V&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Iatraliptice':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=LW24pXXXXXXXXXXXTfa28U&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Maitrechic':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=ZbpXXXXXXXXXXXy0S&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Neojaune':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=TA3XXXXXXXXXXXeBJlvc&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'OreilledeLapin':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=uqTXXXXXXXXXXXzf0Eg&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Princecalme':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=3phXXXXXXXXXXXvO4M4lZ9&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
default:
console.log(`Sorry, we are out of ${Selectdb}`)
}
// Is this the right thing to fetch?
fetch(SelectDB)
.then((response) => response.text())
.then((txtResponse) => {
data = txtResponse;
console.log(data);
data = csvJSON(data);
const tbody = document.querySelector('#user-table tbody');
tbody.innerHTML = '';
data.forEach( (user) => {
const entry = document.createElement('tr')
entry.innerHTML = `
<tr>
<td class="column1">${user.Date}</td>
<td class="column2">${user.Email}</td>
<td class="column3">${user.BounceCode}</td>
<td class="column3">${user.BounceType}</td>
</tr>
`;
tbody.appendChild(entry);
});
}
解决方案
我想你最好用更适合你的情况的东西替换开关盒。您可以在常量变量中排除基本 API URL
const DATABASE_API = 'https://api7.esv2.com/v2/Api'
然后用 apiKeys 创建单独的 key=value 对象是有意义的
const DB_API_KEYS = {
Desclopinette: 'v6PjXXXXXXXXXXX8y682',
Chrysolum: 'utxyiwq1K8S04WauVIa0',
// and so on...
}
之后你就可以使用完整的 url 构造了
const dbApiKey = DB_API_KEYS[SelectDB]
if (dbApiKey === undefined) {
console.log(`Sorry, we are out of ${Selectdb}`)
// throw or something
}
const url = `${DATABASE_API}/Bounces?apiKey=${dbApiKey}&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
最后
fetch(url)
推荐阅读
- sed - SED 替换特定位置的空格
- css - 使用 pandoc 将 Markdown 转换为 PDF 时如何设置页码样式?
- xslt - DNN - 将窗格添加到 DDR 菜单项
- python - 对名称修改目的的总结是否正确?
- php - PHP file_get_contents() 不工作并显示 gethostbyname failed 错误。如何解决?
- reactjs - Apollo useReactiveVar 抛出 TypeError: rv.onNextChange is not a function
- web - 从网站打开并向身份验证应用程序发送数据
- winapi - WM_MOUSEMOVE / wParam 不包含键状态,仅当按下任何鼠标按钮时
- html - 内容不会将大小调整为 pagewrapper
- javascript - 使用 HTML + JS 进行动态搜索