0

I am running my frontend in Node js and backend in Jetty.I am trying to post data from frontend using angular JS but its not working for me .Can anyone suggest what could be the issue? Node Js running in port 3000 and jetty running in 9011. Here is the code snippet:

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

        app.controller("loginctrl", function($scope,$http) {
        app.config(['$sceDelegateProvider', function($sceDelegateProvider) {
            $sceDelegateProvider.resourceUrlWhitelist(['self', 'http://localhost:9011/**']);
             }]);

                $scope.login = function(e){
                    console.log('clicked login...');
                    e.preventDefault();                 

                    $http({
                              url: "http://localhost:9011/test/dummy/doStuff1",
                              method: "POST",
                              data : {username: $scope.userName, password: $scope.password},
                              headers: { 'Content-Type': 'application/json' },
                              withCredentials: false,
                              }).success(function (data, status, headers, config) {
                                    console.log('status',status);
                                    console.log('data',status);
                                    console.log('headers',status);
                            }); 

                }

        });

In the browser console it shows:

POST http://localhost:9011/test/dummy/doStuff1 
(index):1 XMLHttpRequest cannot load http://localhost:9011/test/dummy/doStuff1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 500.

I am not sure what causing the issue can someone help me on this?

2
  • What is not working? Can you specify? Commented Mar 14, 2015 at 16:25
  • The POST is not able to invoke the backend module. I am not sure whats causing the issue.Can you please help me to ascertain the cause. Commented Mar 14, 2015 at 17:05

1 Answer 1

1

If you are hosting your angular app on node(localhost:3000) and trying to make an ajax request to jetty(localhost:9011) you are going to have CORS(http://enable-cors.org/) issues. By default the browser won't let you make a request to a different domain. You need to enable CORS on the server and in your angular app.

Server side obviously depends on the framework you're using. An example using Jersey: http://blog.usul.org/cors-compliant-rest-api-with-jersey-and-containerresponsefilter/

Then enable CORS in Angular(this may not be necessary in your case... it is required if you load templates from a different domain):

//Allow CORS requests to these urls:
$sceDelegateProvider.resourceUrlWhitelist([
   'self',
   'http://localhost:9011/**'
]);
Sign up to request clarification or add additional context in comments.

5 Comments

I did as you suggested but still its not working.I have updated the Question with the configuration you suggested.Can you please suggest what i am missing?
What are you using for the backend? Did you enable CORS on the backend?
I am running REST service in Jetty in backend. I have enabled the container response filter as you suggested in backend.However the backend is not able to receive the request.I see the filter is invoked but the method whcih i am trying to invoke is not happening.Can you please tell me what i am missing?
I have edited the question. The issue is still persisting but the error message is different. Can you please tell me what could be the issue?
It looks like CORS is not setup properly on the backend. The message is telling you to set the Access-Control-Allow-Origin header. Make sure that is set to the domain you are requesting from(localhost:3000) and you should enable it for OPTIONS requests because the browser will do a preflight(OPTIONS) request when it isn't a simple request.

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.