lab1-step3-使用allow_any_origin无法解决跨域问题

use actix_web::{get,web,Result};
use actix_cors::Cors;
use actix_web::http::header;


#[get("/echo/{user_name}")]
async fn echo(path: web::Path<String>)-> Result<String>{
    let user_name=path.into_inner();
    return Ok(format!("Hello, {}",user_name));
}

#[actix_web::main]
async fn main() -> std::io::Result<()>{
    use actix_web::{App,HttpServer};

    HttpServer::new(|| {
        let cors = Cors::default()
            .allow_any_origin()
            .allowed_methods(vec!["GET", "POST"])
            .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
            .allowed_header(header::CONTENT_TYPE);

        App::new()
            .wrap(cors)
            .service(echo)
    })
    .bind(("127.0.0.1", 8081))?
    .run()
    .await
}

以上方法,无法解决跨域问题
报错
求佬帮助!!!

其实使用 Cors::permissive() 既可。

这里的用例的话,可能是有 header 没有补充上,尝试一下使用 allow_any_header()

哦我找到问题了。

你绑定了本地环回地址

在绑定 127.0.0.1 的时候,实际上你的应用程序只会在 loopback 上监听。你需要绑定 0.0.0.0 (监听全部网卡)或者绑定 192.168.199.122 (希望暴露出来的网卡)来实现对该子网的监听。