【问题标题】:Fetch API post never falling into .thenFetch API 帖子永远不会落入 .then
【发布时间】:2017-03-03 06:59:20
【问题描述】:

这是我的代码,可用于发布数据,但未触发“.then”部分。如何触发“.then”?只有当我在表单为空时提交时才会落入“.then”。

HTML:

<form>
                <input name="username" class="no-top input" type="text" placeholder="username"></input>
                <input name="password" class="input" type="password" placeholder="password"></input>
                <div class="table">
                    <div class="table-row">
                      <div class="table-cell">
                        <button class="button" type="submit" value="Login" onclick="post.account.login()"></button>
                      </div>
                      <div class="table-cell">
                        <div class="box">Forgot password?</div>
                        <div class="box">Request access</div>
                      </div>
                    </div>
                </div>
              </form>

JAVASCRIPT:

const request = new Request('http://localhost:8000/account/access', {
        method: 'POST',
        headers: header,
        redirect: 'manual'
        body: JSON.stringify({
          email_address: document.getElementsByName('email_address')[0].value,
          created_date: now()
        })
      })
  fetch(request).then((response) => {
    alert('test')
    if (response.status >= 400) throw new Error("Bad response from server")
    if (response.status == 200)
      return response.json()
  }).then((response) => {
    console.log('success')
  })

【问题讨论】:

  • 然后会在你请求的url返回响应后自动触发,你需要手动触发
  • 在链的末尾添加一个.catch(e =&gt; console.log(e)) - 然后你会看到错误(还要检查 Web 开发人员工具控制台和网络选项卡) - 我猜这是 CORS 问题
  • 对不起,您不需要手动触发。那么
  • 我不确定你,但我个人认为我遇到了这个错误:bugs.chromium.org/p/chromium/issues/detail?id=244910

标签: javascript fetch fetch-api


【解决方案1】:

试试这个..

  var myRequest = new Request('demo.php', {method: 'POST', body: '{"foo":"bar"}'});

  fetch(myRequest)
.then(function(response) {
    if(response.status == 200) //return response.json();
    console.log(response);
    else console.log('Something went wrong on api server!');
})
 .catch(function(error) {
    console.error(error);
});

【讨论】:

  • 它掉进去了,但又停止了随机掉入.then
【解决方案2】:

如果你像这样将 .catch 添加到 .then 链中

const request = new Request('http://localhost:8000/account/access', {
    method: 'POST',
    headers: header,
    redirect: 'manual'
    body: JSON.stringify({
        email_address: document.getElementsByName('email_address')[0].value,
        created_date: now()
    })
})
fetch(request).then((response) => {
    alert('test')
    if (response.status >= 400) throw new Error("Bad response from server")
    if (response.status == 200)
        return response.json()
}).then((response) => {
    console.log('success')
}).catch(function(err) {
    console.log(err);
});

你会看到错误是什么

另外,请查看您的浏览器开发者工具控制台 - 我敢打赌您会看到一些关于 CORS 的错误

【讨论】:

  • 这很奇怪。我正在发布到数据库,但没有收到 cors 错误。但该死的东西并没有落入'.then'
  • 它是否“落入” .catch - 即是否有任何内容记录到控制台
猜你喜欢
  • 2016-06-30
  • 2017-11-28
  • 2021-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多