0

HI i have a micro service running on port 8501.

@RestController
@RequestMapping("/feeds")
public class FeedsController {

    @RequestMapping(method = GET)
    ResponseEntity<?> findAllFeeds() {
        return new ResponseEntity<String>("Hello", OK);
    }


}

when i add url http://localhost:8501/feeds, browser displays "Hello". Now i am trying to access this through angularjs get call

in my AngularJs my code is

'use strict';

var app = angular.module('commentsjsApp');
app.controller('MainCtrl', function ($scope,$http) {

    $http.jsonp('http://localhost:8501/feeds').success(function(data, status, headers, config){
    console.debug("Data : "+data+ "Status");
}).error(function(){
    console.debug("error");
});

EDIT : In Network tab (firebug) i can see the GET with 200 status and response is "Hello". Why i am getting the error in console then? Can any one kindly help me.

and when i run this angularjs app. the following output on console as shown in image

enter image description here

2
  • If your html page is not being served from localhost:8501, then I think you are running against restrictions on fetching data across domains. You may need CORS or jsonp to transfer your data. Commented Dec 11, 2013 at 8:09
  • @musically_ut thanks for response, i have now updated my question. can you kindly check it now Commented Dec 11, 2013 at 8:46

3 Answers 3

1

You are requesting a JSONP data, but the server is returning a string. Return a proper JSON object from the server to fix the issue.

Note: Hello world is not valid JSON, but "Hello World" is.

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

1 Comment

I have Updated the question, as i can see the "Hello" in response (when check it in the network tab of firebug)
1

You need to add the header 'Access-Control-Allow-Origin' in the response, since your calling from localhost:9000 to localhost:8501 (they are technically different servers).

@RequestMapping(method = GET)
ResponseEntity<?> findAllFeeds(HttpServletRequest httpRequest,
        HttpServletResponse httpResponse) {

    httpResponse.addHeader("Access-Control-Allow-Origin",
            "http://127.0.0.1:9000");        

    return new ResponseEntity<String>("Hello", OK);
}

Comments

0

Sometimes Angular requires the colon in the URL to be quoted, try this:

$http.get('http://localhost\\:8501/feeds').success(function(data, status, headers, config){
        console.debug("Data : "+data);
    }).error(function(){
        console.debug("error");
    });

1 Comment

You can try to return properly formatted JSON from server, something like return new ResponseEntity<String>("{value: 'Hello'}", OK);.

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.