2016-07-24 17 views
0

我想重现这一卷曲请求的行为:使用JavaScript提取API执行POST请求

➜ % curl --data "" https://api.t411.ch/auth 
{"error":"User not found","code":101} 

在这种情况下,服务器送我回去JSON。

我在JavaScript中使用的代码是:

fetch('https://api.t411.ch/auth/', { 
    method: 'POST' 
}).then(response => { 
    return response.json(); 
}).then(datas => { 
    console.log(datas); 
}); 

就这样,我得到一个解析JSON的错误,所以,我决定回response.text()而不是response.json()

console.log(datas)打印:string(5) "1.2.4" Service 'view' wasn't found in the dependency injection container

与我的浏览器(GET请求)访问url时获得的字符串相同:https://api.t411.ch/auth

这意味着,我的JavaScript代码发送GET请求,甚至与method: 'post'

我在做什么不好? PS:我认为它根本没有关系,但是我使用了babel在电子项目中传输的es6/jsx。

感谢

+0

警告的话:这是实验性的JavaScript和很差的支持。除了摆弄外,不要使用它,只要使用标准的'XMLHttpRequest'。 – Damon

回答

1

你的代码试图张贴到https://api.t411.ch/auth/。应该是https://api.t411.ch/auth。这应该工作得很好:

fetch('https://api.t411.ch/auth', { 
    method: 'POST' 
}).then(response => { 
    return response.json(); 
}).then(datas => { 
    console.log(datas); 
}); 
+0

哦,谢谢!并说,我花了两个多小时的“/”。 – Epitouille