Cross-origin request for local file

后端 未结 4 1931
无人及你
无人及你 2021-01-11 12:35

I need to open a local html file in the browser. The javascript works fine but ajax stops working and XMLHttpRequest gives a cross origin error. Is there a way to run ajax f

4条回答
  •  醉话见心
    2021-01-11 13:02

    If you are able to change the server code, you can try adding the string "null" to allowed origins. In Chrome, it sends the origin as "null" if it's running from a local file.

    Here's an example in ASP.NET Core for setting up the "null" origin:

    services.AddCors(options =>
    {
        options.AddPolicy("InsecurePolicy",
            builder => builder
                .WithOrigins("null")
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());
    });
    

    Note that this is not recommended, but might be good enough if you just need it during development.

提交回复
热议问题