Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.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 我如何使用Fetch发布x-www-form-urlencoded请求并使用答案?_Javascript_Fetch_X Www Form Urlencoded - Fatal编程技术网

Javascript 我如何使用Fetch发布x-www-form-urlencoded请求并使用答案?

Javascript 我如何使用Fetch发布x-www-form-urlencoded请求并使用答案?,javascript,fetch,x-www-form-urlencoded,Javascript,Fetch,X Www Form Urlencoded,这是我的代码: fetch('http://localhost:3000', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ 'size': 'size_id', 'style': 'style_id', 'qty': '1' }) }) .then(res =>

这是我的代码:

fetch('http://localhost:3000', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: new URLSearchParams({
    'size': 'size_id',
    'style': 'style_id',
    'qty': '1'
  })
})
  .then(res => {
    console.log(res)
  });
我的问题是,我只是得到了'承诺待定'返回。 我对fetch完全陌生,对js也很陌生,所以请不要怪我。

res
是响应数据,而不是响应数据

你在等json吗?然后做:

fetch('http://localhost:3000', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: new URLSearchParams({
    'size': 'size_id',
    'style': 'style_id',
    'qty': '1'
  })
})
  .then(res => res.json())
  .then(res => {
    console.log(res)
  });
否则,获取如下所示的普通响应数据/文本:

fetch('http://localhost:3000', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: new URLSearchParams({
    'size': 'size_id',
    'style': 'style_id',
    'qty': '1'
  })
})
  .then(res => res.text())
  .then(res => {
    console.log(res)
  });

我会试试的,谢谢!