response to preflight request does not access control check : No 'Access-Control-Allow-Origin' header is present on the requested resource
时间: 2023-11-03 19:58:52 浏览: 97
引用: /// 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"字段,从而解决跨域请求的问题。
阅读全文