首页 > 解决方案 > 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)

标签: javascript

解决方案


推荐阅读