在Actix web中使用Cors中间件允许跨域访问时,有时候仍然可能会出现跨域错误。这可能是因为Cors的设置没有生效,或者在使用Cors::permissive时没有正确设置它的with方法。 正确的设置应该类似于以下代码示例:
use actix_cors::{Cors, CorsService};
HttpServer::new(|| {
let cors = Cors::permissive().allow_any_origin().allow_any_method().allow_any_header();
App::new()
.wrap(cors)
.service(your_service)
}).bind("localhost:8080")?
.run()
.await
可以看到,在Cors::permissive()函数后,我们需要为其设置with方法,以指定许可的来源、方法和标头。这样就可以避免出现跨域错误了。