0

I am having difficulty understanding how can I return all methods that are allowed on my HTML page or on my embedded jetty-9.4.4.v20170414 server using Spark framework?

Java Spark code:

options("/panel/data/options", (request, response) -> request
                .headers("Access-Control-Request-Method"));

AngularJS code:

$scope.options = function(){
        $http({
            method: 'OPTIONS',
            url: 'http://localhost:4567/panel/data/options',
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).then(function succesCallback(response){
            alert(response.data)
        });
    };

This is what I have so far.

zero323
  • 322,348
  • 103
  • 959
  • 935
Obito
  • 5
  • 4

1 Answers1

0

First, your Spark java method is modifying the request headers, it should only modify the response headers as your clients are not going to get back the modified request.

options("/panel/data/options", (request, response) -> response
            .headers("Access-Control-Request-Method"));

Then in your angular method, as the back end is changing the headers of your response, then you should read those headers, not the data:

$scope.options = function(){
    $http({
        method: 'OPTIONS',
        url: 'http://localhost:4567/panel/data/options',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function succesCallback(response){
        alert(response.headers)
    });
};
Gonzague
  • 332
  • 3
  • 16
  • Hello, thanks for your answer but sadly that didn't work for me since response.headers() method requires two parameters, and it's return type is void so I can't for example return only response.headers("Access-Control-Request-Method", "*"), I tried just return "OK" with setting headers in response but that doesn't seem to work either. :/ – Obito May 17 '18 at 21:47
  • If you're trying to enable CORS, you can follow this piece of code: https://gist.github.com/saeidzebardast/e375b7d17be3e0f4dddf – Gonzague May 19 '18 at 04:38
  • I am just trying to show list of all allowed HTTP methods in an alert box. Thanks for the answer, I'll try that ^_^ – Obito May 19 '18 at 19:18