Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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

share|improve this question
    
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. – musically_ut Dec 11 '13 at 8:09
    
@musically_ut thanks for response, i have now updated my question. can you kindly check it now – Shahzeb Dec 11 '13 at 8:46

3 Answers 3

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.

share|improve this answer
    
I have Updated the question, as i can see the "Hello" in response (when check it in the network tab of firebug) – Shahzeb Dec 11 '13 at 13:24

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);
}
share|improve this answer

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");
    });
share|improve this answer
    
i have updated my question. Kindly check it. thanks – Shahzeb Dec 11 '13 at 8:46
    
You can try to return properly formatted JSON from server, something like return new ResponseEntity<String>("{value: 'Hello'}", OK);. – remigio Dec 11 '13 at 15:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.