我想重现此cURL请求的行为:
➜ % curl --data "" https://api.t411.ch/auth
{"error":"User not found","code":101}
在这种情况下,服务器会将Jsen发回给我。
我在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时,它与我获得的字符串相同。
这意味着即使使用method: 'post'
我做得不好?
PS:我认为它根本没有关系,但我在电子项目中使用了babel编译的es6 / jsx。由于
答案 0 :(得分: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);
});