【发布时间】:2016-11-28 04:14:45
【问题描述】:
我想重现这个 cURL 请求的行为:
➜ % 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
这与我使用浏览器访问 url 时得到的字符串相同:https://api.t411.ch/auth(GET 请求)。
这意味着我的 javascript 代码发送了一个 GET 请求,即使是 method: 'post'
我做错了什么?
PS:我觉得完全没有关系,但是我在一个electron项目中使用的是babel转译的es6/jsx。
谢谢
【问题讨论】:
-
警告:这是实验性的 javascript,支持很差。除了摆弄外,不要将其用于任何其他用途,只需使用标准的
XMLHttpRequest。 -
点击该链接时收到安全警告。
标签: javascript post fetch-api