0

I have an AngularJS web application with a RESTful Jersey Api as Backend. I'm making a call to this API

function Create(user) {
        return $http.post('http://localhost:8080/NobelGrid/api/users/create/', user).then(handleSuccess, handleError('Error creating user'));
    }

This is the code of the API (POST):

/**
 * This API create an user
 * 
 * @param data
 * @return
 */
@Path("create")
@POST
@Produces("application/json")
public Response create(String data) {

    UserDataConnector connector;
    JSONObject response = new JSONObject(data);

    User userToCreate = new User(response.getString("surname"), response.getString("name"),
            response.getString("mail"), response.getString("username"), response.getString("password"), 0);

    try {

        connector = new UserDataConnector();
        connector.createUser(userToCreate);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return Response.status(Response.Status.OK) // 200
            .entity(userToCreate)
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia,Authorization")
            .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT").build();

}

/**
 * CORS compatible OPTIONS response
 * 
 * @return
 */
@Path("/create")
@OPTIONS
public Response createOPT() {

    System.out.println("Called OPTION for create API");
    return Response.status(Response.Status.OK) // 200
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia,Authorization")
            .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS").build();
}

I've added an OPTION API for create in order to make that API CORS-compatible. In fact the API works well cause the OPTIONS API is called before the POST one and the user is created in my Database. Anyway on front end side I get this error:

XMLHttpRequest cannot load http://localhost:8080/NobelGrid/api/users/create/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 500.

Can anyone please help me?

UPDATE:

stack suggests this question No Access-Control-Allow-Origin header is present on the requested resource as possible duplicate but that solution doesn't work for me cause addHeader(String) is not present in Response Jersey API.

UPDATE 2

I solved the issue using this solution:

http://www.coderanch.com/t/640189/Web-Services/java/Access-Control-Origin-header-present

But I have another error. I will do another question cause I think it's a different argument.

Thanks in advanced!

Community
  • 1
  • 1
Pietro Fragnito
  • 327
  • 1
  • 4
  • 18
  • 1
    Possible duplicate of [No Access-Control-Allow-Origin header is present on the requested resource](http://stackoverflow.com/questions/20881532/no-access-control-allow-origin-header-is-present-on-the-requested-resource) – haihui Feb 19 '16 at 12:06
  • 1
    Take a look at [this topic](http://stackoverflow.com/questions/32034018/how-to-get-rid-of-no-access-control-allow-origin-header-is-present-on-the-requ) – haihui Feb 19 '16 at 12:07
  • no `OPTIONS` in the @post response ... also @Post has @Path('create') as opposed to @Path('/create') ... just noting the differences – Jaromanda X Feb 19 '16 at 12:10
  • http://stackoverflow.com/questions/32034018/how-to-get-rid-of-no-access-control-allow-origin-header-is-present-on-the-requ solution doesn't work for me cause addHeader(String) is not yet present in the Java API. – Pietro Fragnito Feb 19 '16 at 14:44
  • It's a little bit strage: I handle the CORS using Access-Control-Allow-Origin, but it says it is not present! – Pietro Fragnito Feb 19 '16 at 15:33

2 Answers2

0

I solved the issue using this solution:

http://www.coderanch.com/t/640189/Web-Services/java/Access-Control-Origin-header-present

But I have another error.

enter image description here

I will do another question cause I think it's a different argument.

Pietro Fragnito
  • 327
  • 1
  • 4
  • 18
0

Use CORS NPM and add as a middleware.

var express = require('express')
var cors = require('cors')
var app = express()

app.use(cors())

app.get('/products/:id', function (req, res, next) {
  res.json({msg: 'This is CORS-enabled for all origins!'})
})

app.listen(80, function () {
  console.log('CORS-enabled web server listening on port 80')
})

------------------------- Add this lines in your app.js -------------------------

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
Connect2sky
  • 287
  • 4
  • 13