Play Framework CORS Headers

后端 未结 3 1768
星月不相逢
星月不相逢 2021-01-02 17:18

I\'m trying to set CORS Headers for my play framework app. Specifically I\'m getting this error

cannot load http://127.0.0.1:9000/. No \'Access-Control-Allow         


        
3条回答
  •  别那么骄傲
    2021-01-02 17:38

    for me it worked after one day (maybe cash or other things)

    application.conf:

    play.http.filters = "filters.Filters"
    
    play.filters.cors {
      # allow all paths
      pathPrefixes = ["/"]
     # allow all origins (You can specify if you want)
     allowedOrigins = null
     allowedHttpMethods = ["GET", "POST"]
     # allow all headers
     allowedHttpHeaders = null
    }  
    

    build.sbt :

    val appDependencies = Seq(
    filters,
    ....
    )
    

    in package filters.Filter :

    package filters;
    
    import javax.inject.Inject;
    import play.mvc.EssentialFilter;
    import play.filters.cors.CORSFilter;
    import play.http.DefaultHttpFilters;
    
    public class Filters extends DefaultHttpFilters {
    
    CORSFilter corsFilter;
    
    @Inject
    public Filters(CORSFilter corsFilter) {
        super(corsFilter);
        this.corsFilter = corsFilter;
       }
    
    public EssentialFilter[] filters() {
        return new EssentialFilter[] { corsFilter.asJava() };
    }
    }
    

    and in my ajax call:

    $.ajax({
        method:'GET',
        url: xxxxxxxx',
        dataType: 'json',
        headers: {'url': yyyyy,
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET, POST, PUT',
        'Access-Control-Allow-Headers': 'Content-Type'
        },
        success: function(data) { 
    ....}); 
    

    i have no more error, in prod and in local environment !! thank you all

提交回复
热议问题