使用基本身份验证时获取错误404

时间:2017-11-02 01:25:16

标签: javascript ajax http-headers authorization fetch-api

我尝试使用Fetch进行GET查询,但在通过授权时我收到错误404。 我不知道为什么会发生这种错误,因为当我使用像Postman这样的工具时服务器响应很好。

Authorization: Basic ...
method: GET
url: https://zammad.zonngo.com/api/v1/ticket_articles

当我使用Javascript fetch API时,会出现错误404。

OPTIONS https://zammad.zonngo.com/api/v1/ticket_articles 404 (Not Found)

我的代码是:

const myHeaders = new Headers();
myHeaders.append('Authorization','Basic ...');
myHeaders.append('Content-Type','application/json');
fetch(URLTICKETS, {
  method: 'GET',
  headers: myHeaders
})
.then(response => {
  console.log(response);
})
.then(error => {
  console.log(error);
});

如果有人能帮助我会很好。

1 个答案:

答案 0 :(得分:2)

网址上的GET方法返回401状态代码,但 OPTIONS方法实际返回404.

 http OPTIONS https://zammad.zonngo.com/api/v1/ticket_articles
 HTTP/1.1 404 Not Found
 Connection: keep-alive
 Content-Encoding: gzip
 Content-Type: text/html; charset=UTF-8
 Date: Thu, 02 Nov 2017 01:37:59 GMT
 Server: nginx/1.10.3 (Ubuntu)
 Transfer-Encoding: chunked
 X-Request-Id: 16f58319-2658-4b0e-824c-08ca68e8d401
 X-Runtime: 0.003135

OPTIONS可能是由https://zammad.zonngo.com/似乎无法正确处理的CORS预检请求引起的。

https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request