I am getting the following error from the browser debugger:
Origin http://localhost:8000 not found in Access-Control-Allow-Origin header.
However I have set the header in my Global.java
file in my API:
Global.java
@Override
public Promise<SimpleResult> call(Http.Context ctx) throws java.lang.Throwable {
Promise<SimpleResult> result = this.delegate.call(ctx);
Http.Response response = ctx.response();
response.setHeader("Access-Control-Allow-Origin", "*");
response.setContentType("application/json");
return result;
}
Here is my the API route I am hitting.
routes
POST /api/users/insertuser @controllers.UserController.insertUser
And here is the controller method:
UserController.java
@BodyParser.Of(BodyParser.Json.class)
public Result insertUser() {
JsonNode json = request().body().asJson();
String email = json.findPath("email").asText();
String username = json.findPath("username").asText();
String password = json.findPath("password").asText();
if(email == null || username == null || password == null) {
return badRequest("Missing parameter[s]");
} else {
User user = new User(username, email, false, password, getDate(), getDate());
repo.insertUser(user);
return getUserByEmail(email);
}
}
Here is my API call from my Angular application:
userapiservice.js
var factory = {};
var baseUrl = 'http://127.0.0.1:9000/api/users/';
factory.insertUser = function (user) {
return $http({
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
url: baseUrl + 'insertuser/',
params: { username: user.username, email: user.email, password: user.password }
});
};
Is there something I am missing/doing wrong? Tried on a couple of browsers, been stuck on it for about a week now, I've been teaching myself as I go along and I've found the Play! documentation to be... okay.
first add preflight request
but remember
call doesn't work on internal server error