gpt4 book ai didi

jquery - 为什么我的服务器忽略来自 ajax 请求的身份验证 header ?

转载 作者:可可西里 更新时间:2023-11-01 16:05:18 24 4
gpt4 key购买 nike

来 self 使用的 JavaScript:

xhr.setRequestHeader("Authorization", make_base_auth(username,password));

但是 HTTP 请求没有 Authorization header :

OPTIONS /restService/index?_=1362589672203 HTTP/1.1
Host: myappinheroku.herokuapp.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-MX,es-ES;q=0.8,es-AR;q=0.7,es;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Origin: http://127.0.0.1:8081
Access-Control-Request-Method: GET
Access-Control-Request-Headers: authorization,content-type
Connection: keep-alive

似乎完全忽略了身份验证。怎么了?我们如何为 CORS 启用身份验证?


这是服务器对上述请求的响应:

HTTP/1.1 401 Full authentication is required to access this resource
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: origin, authorization, accept, content-type, x-requested- with
Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Server: Jetty(7.x.y-SNAPSHOT)
Set-Cookie: JSESSIONID=6smxjnlqelmc1lg98ain16wv7;Path=/
WWW-Authenticate: Basic realm="Ralph's Bait and Tackle"
Transfer-Encoding: chunked
Connection: keep-alive

最佳答案

Access-Control-Allow-Credentials。您需要将 Access-Control-Allow-Origin 设置为 Origin 本身的值(即本例中的 http://127.0.0.1:8081)。

另请注意,不会在预检请求中发送身份验证凭据。它们仅在实际请求时发送。预检仅用于验证是否允许 CORS 请求,它本身不应进行任何身份验证。

关于jquery - 为什么我的服务器忽略来自 ajax 请求的身份验证 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15253957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com