Question

I have a RESTapi with the following methode:

@Path("/banksummary")
public class BankSummaryRestService {
    Database db = new DatabaseAccessOutput();

    @GET
    @Path("/")
    public String printMessage() throws Exception {
        Summary result;

        try {
            result = db.getBankSummary();

        }
        catch (Exception e) {
            throw e;
        }

        Gson gson = new Gson();
        String json = gson.toJson(result);


        return json;
    }
}

When I go to localhost:8080/html5/api/1.0/banksummary I see the output.

In my AngularJS project I have the following code: In service.js:

dashboardAPI.getBankSummary = function() {
        return $http(
        {
            method: 'GET',
            url: 'localhost:8080/html5/api/1.0/banksummary'
        }
        );
    }

In the controller:

dashboardAPIService.getBankSummary().success(function (response) {
        $scope.summary = response;
    });

When I used a local json file it worked, but now I use the URL to the RESTapi (local file had to be replaced by the RESTapi) and I get the following error:

XMLHttpRequest cannot load localhost:8080/html5/api/1.0/banksummary. Cross origin requests are only supported for HTTP. angular.min.js:76
Error: A network error occurred.

Anyone that knows what I'm doing wrong?

Was it helpful?

Solution

You need to specify the protocol (in this case, http) in your URL:

dashboardAPI.getBankSummary = function() {
    return $http(
    {
        method: 'GET',
        url: 'http://localhost:8080/html5/api/1.0/banksummary'
    }
    );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top