gpt4 book ai didi

jquery - Ajax 调用不同域 URL 时出现 401 预检错误

转载 作者:行者123 更新时间:2023-12-01 04:44:03 25 4
gpt4 key购买 nike

我的请求显示在 Chrome 网络 XHR 工具上。

General:
Remote Address:127.0.0.1:80
Request URL:http://ws.uvx/app_dev.php/api/venuelogin
Request Method:OPTIONS
Status Code:400 Bad Request

Response Headers:
view source
Access-Control-Allow-Headers:x-custom-auth
Access-Control-Allow-Methods:POST, PUT, GET, DELETE, OPTIONS
Access-Control-Allow-Origin:http://localhost:8383
Access-Control-Max-Age:3600
Cache-Control:no-cache, max-age=604800
Connection:close
Content-Encoding:gzip
Content-Length:52
Content-Type:text/html; charset=UTF-8
Date:Wed, 23 Sep 2015 07:59:04 GMT
Expires:Wed, 30 Sep 2015 07:59:04 GMT
Server:Apache
Vary:Accept-Encoding
X-Debug-Token:8cd64c
X-Debug-Token-Link:/_profiler/8cd64c
X-Powered-By:PHP/5.5.10

Request Headers:
view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:es-ES,es;q=0.8,en;q=0.6
Access-Control-Request-Headers:accept, content-type, p
Access-Control-Request-Method:GET
Cache-Control:no-cache
Connection:keep-alive
Host:ws.uvx
Origin:http://localhost:8383
Pragma:no-cache
Referer:http://localhost:8383/UVox%20Mobile/jwt-demo/venuelogin.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36

我的 Ajax 调用:

jQuery.ajax({
url: "http://ws.uvx/app_dev.php/api/venuelogin",
type: "GET",
headers: {
"p": "1441",
"Content-type": "application/json"
}
})

以及 Symfony2 cors 配置:

nelmio_cors:
defaults:
allow_credentials: false
allow_origin: []
allow_headers: []
allow_methods: []
expose_headers: []
max_age: 0
hosts: []
origin_regex: false
paths:
'^/api/':
allow_origin: ['*']
allow_headers: ['X-Custom-Auth']
allow_methods: ['POST', 'PUT', 'GET', 'DELETE','OPTIONS']
max_age: 3600

如果我将allow_headers: ['X-Custom-Auth']更改为allow_headers:['*'],那么我会收到500错误。

我还按照 PAW(我在其中检查 API 请求的 OSX 应用程序)的建议尝试了 Content-Type:application/octet-stream。事实上,这个请求是来自 PAW 的 200 OK。

同时执行 CURL,我得到了带有 200 OK 的 JSON。

我缺少什么?非常感谢。

最佳答案

首先要检查的是您没有重写服务器上的 header (启用站点的配置)。因此,请查看它们并注释任何“Allow-Origin”行。

第二步是在你的应用程序上(在我的例子中是 Symfony):

a) 我的 FOSRESTBundle 配置在以下行的 json 之前包含 html:

format_listener:
rules:
- { path: ^/, priorities: [ json, xml, html ], fallback_format: json, prefer_extension: true }

所以我被迫在 url 前面加上 .json 前缀,或者更新如下所示的行,否则服务器会尝试返回 html,并且因为我没有为其创建模板,所以我收到了 500 错误。

b) 最后一步是避免在routing.yml 中“单独”声明路由,并将其作为“手动”fosrestbundle 路由注释包含在用于休息类型路由的 Controller 上。

api_venues:
type: rest
resource: Acme\DemoBundle\Controller\Api\VenueController

关于jquery - Ajax 调用不同域 URL 时出现 401 预检错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32734289/

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