Ich habe zwei Anwendungen laufen unabhängig, eine kümmert sich um mein Backend (geschrieben in Scala Play) dann andere ist mein Frontend (Angular mit einem statischen Node-Server).Verwendung von Cors über zwei unabhängig voneinander laufende lokale Anwendungen
Ich versuche Daten über mein Frontend über ein Formular von meiner Scala Play App anzufordern.
this.insertionOrder = function(){
$http({
method: 'POST',
url: '//localhost:9000/insertsupplier',
header: {
'Content-type': 'application/json',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS'
},
data:{
'supplier_id': 1,
'suppliername': 'xxx',
'supplier_address': 'xxx xxx xxx xxx',
'contact': '[email protected]',
'datecreated': '2017-10-15T09:45:00.000UTC+00:00'
}
}).then(function(response){
console.log(response);
return response.data
}, function(err){
console.log(err)
});
};
und mein Spiel App wie folgt aussieht:
Controller:
def insertsupplier = Action(parse.json) { implicit request =>
val json = request.body
val sup: Supplier = json.as[Supplier]
sup.insertSql(con)
Ok("test")
}
meine build.sbt enthält Filter:
libraryDependencies ++= Seq(
cache ,
ws,
jdbc,
filters
)
und die MyFilters.scala
class MyFilters (implicit inj:Injector) extends HttpFilters with Injectable {
implicit val as = inject[ActorSystem]
implicit val mat = ActorMaterializer()
val gzip = new GzipFilter()
val csrf = inject[CSRFFilter]
val cors = inject[CORSFilter]
//println(s"csrf: ${csrf.tokenProvider}")
//println(s"csrf: ${csrf.tokenProvider.generateToken}")
def filters = Seq(gzip,cors,csrf)
}
und schließlich meine application.conf
play.filters.cors {
pathPrefixes = ["*"]
allowedOrigins = ["http://localhost:3000","https://localhost:3000","http://localhost:3000/*","https://localhost:3000/*"]
allowedHttpMethods = ["GET", "POST", "OPTIONS"]
allowedHttpHeaders = ["Accept"]
# preflightMaxAge = 1 hour
}
play.filters.csrf {
cookie.name = "XSRF-TOKEN"
header.name = "X-XSRF-TOKEN"
}
play.http.filters = "filters.MyFilters"
Ich erhalte den Fehler "XMLHttpRequest cannot load http://localhost:9000/insertsupplier. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 500."
Ich fühle, dass zuerst falsch mein CORS Setup ohnehin ->Was geändert werden muss? Ich bin neu dazu.
Und kann ich sogar cors verwenden, um auf Daten von localhost zuzugreifen?
Vielen Dank für Ihre Antwort. Es ist ein sehr nützlicher Einblick in ein Thema, das ich bisher nur leicht berührt habe. Ich werde versuchen, Ihre Kommentare zu berücksichtigen und daraus eine Lösung zu erstellen. – Dribel