Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取请求在控制台或前端不返回任何数据_Javascript_Fetch - Fatal编程技术网

Javascript 获取请求在控制台或前端不返回任何数据

Javascript 获取请求在控制台或前端不返回任何数据,javascript,fetch,Javascript,Fetch,当我提交表单时,我没有返回任何数据,甚至在控制台中也没有。我正试图从WHOIS处返回有关搜索URL的详细信息,但没有得到任何回复 有谁能提供一些建议,说明为什么会出现这种情况 这是我的前端脚本标签,在我的表单之后: document.getElementById('search').addEventListener('submit', function(e) { e.preventDefault(); getDetails(); }) async function getDetails(url

当我提交表单时,我没有返回任何数据,甚至在控制台中也没有。我正试图从WHOIS处返回有关搜索URL的详细信息,但没有得到任何回复

有谁能提供一些建议,说明为什么会出现这种情况

这是我的前端脚本标签,在我的表单之后:

document.getElementById('search').addEventListener('submit', function(e) { e.preventDefault(); getDetails(); })
async function getDetails(url = `http://localhost:3000/lookup/${url}`, data = {}) {
  return new Promise(function (resolve, reject) {
    fetch(url, {
      method: 'GET',
      headers: {
        'Content-Type': 'text/html'
      },
      body: JSON.stringify(data)
      }).then(async response => {
        if (response.ok) {
          response.json().then(json => resolve(json))
          console.log(data);
        } else {
          response.json().then(json => reject(json))
        }
      }).catch(async error => {
        reject(error)
      })
  })
} 
在我的express后端,我正在使用
req.params.url
,如果这有助于提供任何上下文


我的状态代码是200,标题选项卡中的所有显示都正常…

您错误地使用了async/wait。试试这个:

async function getDetails(url = `http://localhost:3000/lookup/${url}`, data = {}) {
  const response = await fetch(url, {
    method: 'GET',
    headers: {
      'Content-Type': 'text/html'
    },
    body: JSON.stringify(data)
  });
  const json = await response.json();
  if (response.ok) {
    console.log(json);
    return json;
  } else {
     throw new Error(json);
  }
}

本质上,
await
是对
的替代(但您只能在标记为
async
的函数中使用它)。

您混合使用了promise和async语法,这很容易混淆,让我们首先将其翻译为
promise
然后将
转换为
await
(如果您可以使用
async
,那么就这样做,这比
Promise
/
那么
更简单):

document.getElementById('search').addEventListener(
“提交”,
函数(e){
e、 预防默认值();
getDetails();
});
异步函数getDetails(url=`http://localhost:3000/lookup/${url}`,数据={})
{
//如果Fetch无法连接到服务,它将引发异常
const response=等待获取(url{
方法:“GET”,
标题:{'Content Type':'text/html'},
正文:JSON.stringify(数据)
});
if(response.ok)
return wait response.json();
//我们可以连接,但服务返回错误
//可能没有响应主体,因此response.json()或.body()可能会崩溃
抛出新错误(`errorfromserver${response.status}:${response.statusText}`);
//我们不需要捕获,因为等待中的任何异常都会层叠出现
} 
这使得它更具可读性,而且很明显,
getDetails
本身不做任何更改,它只是从服务返回JSON。修复程序需要位于事件侦听器中-它需要对该结果执行一些操作:

document.getElementById('search').addEventListener(
“提交”,
异步e=>{
e、 预防默认值();
const searchResult=等待获取详细信息();
//做一些事情来显示结果,填充#结果
const resultsElement=document.getElementById('results');
resultElement.innerText=JSON.stringify(searchResult);
});

是否正在运行
getDetails.then()
?getDetails返回一个承诺,因此需要执行promise@Sam好的……我知道如果你没有在函数中使用
wait
,它就不应该被标记为
async
。或者你可以重构该函数,使其不使用承诺(而是使用
await
)@Derek我该怎么做>下面是一个基本异步函数的示例:
异步函数xyz(url){try{const response=await fetch(url);return response.json();}catch(e){console.log(error);};
在这个上下文中没有
resolve
函数,您只想
返回json;
修复了返回的问题。