Fetch API post永远不会落入.then
Fetch API post never falling into .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')
})
试试这个…
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链中添加。catch
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的错误
相关文章:
- 如何更改bigquery API中的计费层选项
- Amazon S3 REST API大小不正确
- 客户端服务器REST API captcha实现
- 使用Facebook live API创建实时视频对象时的隐私设置
- 谷歌放置API:按国家或餐馆名称搜索餐馆
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 是否可以控制获取哪些Google地图脚本(JavaScript API)
- API密钥使用和检查示例
- 谷歌地图API v3不适用于移动浏览器或PhoneGap
- 任何方式使AJAX调用Gmail API,而无需通过JS库
- 谷歌地图JS API+JSON-多个标记没有显示
- 单击超链接时,如何使用Google Maps API v3缩放地图
- 如何在 API 调用后和 if 语句中启用提交按钮
- 与运行长作业(javascript,node.js)的第三方API同步的最佳实践
- 搜索api在mac上显示对话框
- Backbone.js restful json API design
- 标记的实时更新,无需加载页面谷歌地图API V3
- 是否可以使用iframe API在iOS浏览器上播放youtube视频
- Fetch API post永远不会落入.then
- 获取api - get json主体在then和catch块为单独的状态码