0

Using AngularJS, and the CXF-JAXRS library running within an OSGi environment (KARAF), I'm trying to get a simple API response working.

The logs show that AngularJS is properly connecting to the REST service, and is properly being responded to with a 200 status code. But, in AngularJS, the information sent back isn't being retrieved.

FYI: I had to take the "http://" out of all links because StackExchange tells me I don't have enough reputation to include them. But they're there in the actual code/logs.

REST Method I'm connecting to

@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Response test(){
    String testResponse = "Success.";
    Response responseString = Response
            .status(200)
            .entity(testResponse)
            .build();
    return responseString;
}

AngularJS Controller

        app.controller('HttpController', function($scope, $http){
        $http({
            method: 'GET',
            url: 'localhost:8181/cxf/strsoftware/test'
        }).then(function successCallback(response){
                $scope.testString = response.data;
        }, function errorCallback(response){
                $scope.testString = response;
        });
        $scope.validationString = "Controller is functional.";
    });

AngularJS Display Code

<div ng-controller="HttpController">
The response is {{ testString }}<br>
{{validationString}}

Display On AngularJS HTML Page

KARAF Log Displayed When Connecting

2017-01-10 11:42:30,133 | INFO | qtp84694963-897 | LoggingInInterceptor | 107 - org.apache.cxf.cxf-core - 3.2.0.SNAPSHOT | Inbound Message

ID: 20 Address: localhost:8181/cxf/strsoftware/test Http-Method: GET Content-Type:

Headers: {Accept=[application/json, text/plain, /], accept-encoding=[gzip, deflate, sdch, br], Accept-Language=[en-US,en;q=0.8], connection=[keep-alive], Content-Type=[null], Host=[localhost:8181], Origin=[localhost:63343], Referer=[localhost:63343/STRFrontEnd/StartScreen.html?_ijt=qsu1c8a1qskj0def9ho1rml4hv], User-Agent=[Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36]}

2017-01-10 11:42:30,135 | INFO | qtp84694963-897 | LoggingOutInterceptor | 107 - org.apache.cxf.cxf-core - 3.2.0.SNAPSHOT | Outbound Message

ID: 20 Response-Code: 200 Content-Type: application/json Headers: {Content-Type=[application/json], Date=[Tue, 10 Jan 2017 16:42:30 GMT]}

Payload: Success.

UPDATE

By opening the Inspect page on Chrome (Ctrl+Shift+I) and clicking 'Console', we discovered the following error:

angular.js:11756XMLHttpRequest cannot load localhost:8181/cxf/strsoftware/test. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

This is a Server-Side error, so we're currently implementing CORS into the Server-Side to see if that fixes the problem. Will post solution when it's found.

5
  • Are you sure there is not any problem at server side? put some logs in successCallback & errorCallback function to see which one will call.share with me your result. Commented Jan 10, 2017 at 17:18
  • @Shadmehr Fairly certain, because the Karaf logs indicate the request was responded to with Status Code 200. I changed the successCallBack function to write "Success" and the errorCallBack function to write "Failure" on the html page when it's launched. Each time it reads "Failure", so the error method is definitely the one that's being triggered. Commented Jan 10, 2017 at 18:37
  • I don't know any idea about Kraft. But I saw Content-Type:null and for these reason I ask you to sure server returns correct format. Commented Jan 10, 2017 at 20:50
  • Ah I see. Ok thank you, I'll look into that tomorrow. Commented Jan 10, 2017 at 21:49
  • I've tested your theory by setting up a simple separate GlassFish server without the OSGi framework that returns a simple plain-text message. Even with that server being the most basic example of a REST interface, and working as intended in the browser, I get the same error message when trying to connect to it with AngularJS. Seems this is definitely client side. Commented Jan 11, 2017 at 13:35

3 Answers 3

1

try this one, if not working replace data : '' with params : {} :

        $http({
            method: 'GET',
            url: 'localhost:8181/cxf/strsoftware/test',

            // Just to be sure! maybe you don't need it
            dataType: 'json',
            headers: {
                "Content-Type": "application/json"
            },

            //To be sure Angular does NOT remove the content-type header.
            data: '',
        }).then(function successCallback(response) {
            $scope.testString = response.data;
        }, function errorCallback(response) {
            $scope.testString = response;
        });

EDIT

View the comments below this answer to see where Shadmehr solved the problem. The solution was to implement a CORS-filter into the server-side part of the application. We were getting a Cross Region error in the browser when trying to access the API.

Sign up to request clarification or add additional context in comments.

3 Comments

Tried, no dice yet.
which version of angular do you use? Do you see any error in console's browser suach as "Cross-Origin Request Blocked"?
Yes :) You hit the mark on that one. Look at the edit I added in to my OP. We're thinking it's a server-side problem at the moment. The guy who made the server-side app is working on implementing CORS.
1

You are calling an anonymous function in the .then(). So it should be like this:

$http({
        method: 'GET',
        url: 'localhost:8181/cxf/strsoftware/test'
    }).then(function(response){
            $scope.testString = response.data;
    }, function(response){
            $scope.testString = response;
    });

Comments

0

Not sure if your js code is correct. I have also experimented with angular js and my code looked like this:

$http.get("/cxf/strsoftware/test")
  .success(function (response) {$scope.testString = response.data;});

See blueprint-cdi-example for my full code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.