Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a strange problem with my project. I decided to write the server application in Spring and a frontend with AngularJS. On the server side, I have a simple controller which return some simple data, from simple Entity ;). It looks like this:

@RestController
public class ApiController
{
    @RequestMapping( value = "/getAllProfiles", method = RequestMethod.GET)
    public Entity getEntity()
    {
        Entity e = new Entity();
        e.setName( "myname" );
        return e;
    }
}

On the front side, I have an Angulars controller, which looks like:

var app = angular.module('app', []);

app.config(function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;   
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
});

app.controller('Hello', function ($scope, $http) {
    $http.get({
        method: 'GET',
        url: 'http://localhost:8080/getAllProfiles'
    }).success(function (data) {
        console.log('success', data)
        $scope.greeting = data;
    }).error(function(){
        console.log("something goes wrong");
    });
})

and html file:

<html ng-app="app">
<head>
    <title>Hello AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script>
    <script src="view1.js"></script>
</head>
<body>
<div ng-controller="Hello">
    <p>The content is {{greeting.name}}</p>
</div>
</body>
</html>

When I start server, I connect to the localhost:8080/getAllProfiles and everything works well. I receive JSON like this {"name":"myname"}. But, when I try to do the same from the Angular side, I receive The content is - an empty data from server and, of course, error function is calling in the controller. I thought it can be a CORS problem, but I added filters on server side, and on the Angular side and it didn't help. The js console (I'm using Chrome) says: Failed to load resource: the server responded with a status of 404 (Not Found)

I really don't know what to do with this. I want "to talk" between this two applications, but I cannot because of this. If someone can help me with this, I will be very thankful.

share|improve this question
    
It probably makes no difference but I see you set a method name of GET in the config of the http.get request, surely you don't need this line, it's already a GET – mindparse Feb 7 '15 at 21:40
    
Yes, I know. Changes between struct of this http do not resolve a problem. – allocer Feb 7 '15 at 21:54
    
If you have checked that CORS is not the issue, have you double-checked too that a) the spring application is listening on 8080 and b) theres no base path that it is bound to (that is, some /foo/getAllProfiles)? – h7r Feb 7 '15 at 21:55
    
Yes, Spring app is on 8080, because when I do this in the chrome - localhost:8080/getAllProfiles I got an answer from the server. It doesn't work from the front side. The path is good, you can see this in the spring controller above. – allocer Feb 7 '15 at 21:59
up vote 0 down vote accepted

If CORS is a problem on localhost,then you can use grunt-connect-proxy. Define in your grunt file:

proxies: [
                {
                    context: '/',
                    host: '127.0.0.1',
                    port: 8080,
                    https: false,
                    xforward: false
                }
            ]

Don't forget to add connext proxy task to your task. On the website is everything clear explained. This would solve cross domain problem in localhost. Then you can call your server in your controller:

$http.get({
        method: 'GET',
        url: '/getAllProfiles'
    })

It would work, because grunt would run proxy on your angular application port.

By the way. Have you tried to test you angular logic through unit tests? Use $httpbackend to simulate exactly the same backend as your Spring application does and you will see the results.

share|improve this answer
    
No, I didn't test it. Thanks for this, I will do it. Can you tell more about this grunt-connect? I need to add it to my angular app and set it in some config? – allocer Feb 7 '15 at 22:01
    
Do you use grunt? It is to grunt - a tool that compiles project for you. I don't know if there is proxy without it. – cyan Feb 7 '15 at 22:11
    
I have installated Grunt into my project. I don't know, maybe I have done something wrong, but I used this tutorial gruntjs.com/getting-started and Readme file from link that you gave in post above. It still doesn't work. I'm using WebStrom to my front app and IntelliJ to server app. When I test my rest service from WebStorm it works correctly (Tools->Test RESTful Service). I have Grunt, I have a Gruntfile and a problem is not resolved. – allocer Feb 7 '15 at 23:03
    
do you have message during starting grunt, that proxy started? can you go to your application port localhost:9000/getAllProfiles and see the data? Have you tested with $httpBackend? – cyan Feb 7 '15 at 23:20
    
Interesting. I run grunt serve from command line, and get: "Running "serve" task Server is running on port 9000..." I have specified proxy in gruntfile file as "proxies: [{ context: '/', host: '127.0.0.1', port: 8080, https: false, xforward: false }]", Have no idea why it doesn't use proxy... – allocer Feb 8 '15 at 0:23

Ok, finally the problem has been solved. Thank you cyan for your help, the Grunt was a solution.

I want to describe this solution for people who do not want to read all stuff in this topic. ;) I wanted to connect from some Angular application to my Spring REST API (2 separated applications), but I couldn't do this because of proxy problem on my localhost. I have installed a Grunt for my Angular application, configured this, started server and tried to get data from it. It came off. Of course, CORS filters on the server side are needed. They are describe here https://spring.io/guides/gs/rest-service-cors/

I use this tutorials which helped me with it: http://www.hierax.org/2014/01/grunt-proxy-setup-for-yeoman.html http://fettblog.eu/blog/2013/09/20/using-grunt-connect-proxy/ and a github's link from cyan.

share|improve this answer

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.