【发布时间】: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 => console.log(e))- 然后你会看到错误(还要检查 Web 开发人员工具控制台和网络选项卡) - 我猜这是 CORS 问题 -
对不起,您不需要手动触发。那么
-
我不确定你,但我个人认为我遇到了这个错误:bugs.chromium.org/p/chromium/issues/detail?id=244910
标签: javascript fetch fetch-api