Request header field x-requested-with is not allowed by Access-Control-Allow-Headers in preflight response
时间: 2023-07-10 17:33:28 浏览: 48
这个错误通常发生在您尝试向不同源的服务器发送 AJAX 请求时。浏览器会发送一个 OPTIONS 请求作为预检请求(preflight request),以查看服务器是否允许跨域请求。如果服务器没有正确配置 CORS,那么浏览器就会抛出这个错误。
在具体实现上,浏览器会检查 `Access-Control-Allow-Headers` 响应头中是否包含了 `X-Requested-With` 字段。如果没有,那么浏览器就会抛出这个错误。
解决这个问题的方法是在服务器端正确配置 CORS。您需要在服务器端设置 `Access-Control-Allow-Headers` 响应头字段,以允许浏览器发送 `X-Requested-With` 字段。例如,您可以在服务器端设置以下响应头字段:
```
Access-Control-Allow-Headers: X-Requested-With, Content-Type, Authorization
```
如果您使用的是 Apache 服务器,您可以在 `.htaccess` 文件中添加以下内容:
```
Header set Access-Control-Allow-Headers "X-Requested-With, Content-Type, Authorization"
```
如果您使用的是其他类型的服务器,您需要查看相关文档以了解如何配置 CORS。
相关问题
vue中axios请求request header field content-type is not allowed by access-con
这个问题通常是由于跨域请求时服务器配置不当导致的。服务器需要在响应头中设置Access-Control-Allow-Headers字段,允许客户端发送Content-Type请求头。
可以在服务器端添加以下代码来解决这个问题:
```python
from flask import Flask, jsonify, make_response
app = Flask(__name__)
@app.after_request
def after_request(response):
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'x-requested-with,content-type'
return response
# 其他接口代码...
```
这里用Flask作为示例,具体的框架可以根据实际情况进行修改。
response to preflight request does not access control check : No 'Access-Control-Allow-Origin' header is present on the requested resource
引用: /// 2、“has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.”. /// 3、“has been blocked by CORS policy: Response to preflight request...。 引用:注意 此种方式可能并不能完全解决 CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true Request header field Access-Control-Allow-Headers is not allowed by itself in preflight response Request header field cache-control is not allowed by Access-Control-Allow-Headers in preflight response. 另一种方法 golang库-cors使用 服务器func StartHttpServer() bool { c := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, }) mux := http.NewServeMux() mux.HandleFunc("/getname", GetIDHandler) handler := c.Handler(mux) http.ListenAndServe(addr, handler) return true } jsfunction GetRandName() { $.ajax({ url: "http://127.0.0.1/getname", type: 'GET', success: function (data) { console.log(data) UpdateNameInput(htmlDomInputID, data.id); }, error: function () { console.log("Get Rand Name Failed!"); } }); }。 引用:错误详情 在使用ajax向http服务器请求时,出现以下错误:Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 请求如下:$.ajax({ url: "http://127.0.0.1/getname", type: 'GET', success: function (data) { UpdateNameInput(htmlDomInputID, data); }, error: function () { console.log("Get Rand Name Failed!"); } }); 服务器为golang实现 一种解决 。
问题:请告诉我,response to preflight request does not access control check : No 'Access-Control-Allow-Origin' header is present on the requested resource
当使用Ajax请求向HTTP服务器发送请求时,如果服务器没有在响应头中包含"Access-Control-Allow-Origin"字段,就会出现"Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource"的错误。这是因为浏览器执行了预检请求(preflight request),并要求服务器在响应头中包含"Access-Control-Allow-Origin"字段,以确保请求的安全性。
为了解决这个问题,你可以通过在服务器端设置响应头来解决。具体来说,在你的服务器端代码中,你可以使用Golang的CORS库来处理跨域请求。在你的"StartHttpServer"函数中,创建一个CORS对象,并将"AllowedOrigins"字段设置为"*",这将允许来自任何域的请求。然后,使用CORS对象的Handler方法来处理HTTP请求。通过这种方式,你的服务器将在响应头中包含"Access-Control-Allow-Origin"字段,从而解决跨域请求的问题。