javascript - getData 返回结果
问题描述
目前,我正在尝试创建一个formResult
何时Submit
被调用,但这是我想我想做的一些GetData.
事情这只是我可以在 getData 之后放置的东西吗?
你如何建议我重新格式化这个?
const form = document.getelementbyid('Submit')
const formResult = new FormData(form);
const url = '/' + encodeURIComponent(formResult.get("search"));
const button = document.getElementById('search');
const sendHTTPSRequest = (method, url, data)
function Submit(e) {
e.preventDefault();
alert(button.value)
}
return fetch(url, {
method: method,
body: JSON.stringify(data),
headers: data ? {' Content Type': 'application/json'} : {}
}).then(response => {
if (response.status >= 200) {
return response.jsomn ();
}
if (response.status >= 500) {
return response.json().then(errResData => {
const error = new Error ('Something went wrong');
error.data = errResData;
throw error;
});
}
return response.json ();
});
const getData = () => {
sendHTTPSRequest('GET', '/url').then(responseData => {
console.log(responseData);
});
};
button.addEventListener('search', getData)
解决方案
推荐阅读
- linux - Extracting string value with multiple space in .sh
- c++ - 增加对 http1.1 的铬的最大请求
- javascript - 操作后如何创建新对象
- java - 我们可以在java中的静态函数中声明一个静态类吗?
- php - 使用 phpdoc 注释函数会在 laravel 的类顶部添加“使用”。它会使应用程序变慢吗?
- openfire - Openfire 安装配置文件设置错误
- css - 使用 Angular Datatable 添加 mat-sort-header 后无法更改字体颜色
- teamcity - TeamCity GitHub 原生代码扫描需要很多时间
- ios - 在 Swift 包管理器上隐藏实现
- google-cloud-platform - 如何使我的日志同步存储桶在 GCP 中持久化