-1

i am try to create a SPA with Angular. I have to consume the a service form different server. It is a java based service. Here i am getting "No 'Access-Control-Allow-Origin' header is present on the requested resource"

angular.module('tableApp', [])

.config(function ($httpProvider) {
    //Enable cross domain calls
    $httpProvider.defaults.useXDomain = true;

    $httpProvider.defaults.withCredentials = true;
    delete $httpProvider.defaults.headers.common["X-Requested-With"];
    $httpProvider.defaults.headers.common["Accept"] = "application/json";
    $httpProvider.defaults.headers.common["Content-Type"] = "application/json";
    //Remove the header used to identify ajax call  that would prevent CORS from working
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
})
.controller('tableCtrl', ['$scope', '$http', '$timeout', function ($scope, $http) {

     var bassURL = 'http://[server ip]:8080/ABC/resource/XYZ'
    $http.get(bassURL + '/getTypes').success(function (Type) {
        $scope.Type = Type;
    });
    }
])

Please help.

user2589824
  • 19
  • 1
  • 8
  • Did you try Googling the error? – SLaks Feb 25 '15 at 18:04
  • Your application looks like it does what it can. Is the app running on the same server as your `bassURL`? Is the server set up to accept requests from external resources? – mudpuddle Feb 25 '15 at 18:05
  • no, base url is different.it able to get data in REST Client from the url. – user2589824 Feb 25 '15 at 18:06
  • Access-Control-Allow-Origin is set on the response from server, not on client request to allow clients from different origins to have access to the response.So you have to setup your server to accept request from client. – itzMEonTV Feb 25 '15 at 18:07
  • It's called CORS, you need to set it up on your server. – ribsies Feb 25 '15 at 18:09

2 Answers2

1

Your browser is using the CORS protocol to verify access to the service, to prevent cross-site scripting attacks. You need to enable CORS support on the server (adding the Access-Control-Allow-Origin header allowing the site where your Angular site is hosted). Alternatively, you need to use one of the pre-CORS hacks for getting JSON cross-site, and your server needs to provide support for it.

antlersoft
  • 14,636
  • 4
  • 35
  • 55
0

Step 1:

Verify that your angular app is actually hitting a valid URL. Do this through Fiddler or Postman

Step 2:

Verify that your client AngularJS application and server are running at the same URL and port.

If you intend on having them running on different ports / URLs, you'll need to setup CORS on your java application as mentioned in other comments.

Step 3 (if using Step 2):

Specify a append a JSON_CALLBACK parameter to your URL.

SO Example
Example

Community
  • 1
  • 1
olingern
  • 494
  • 1
  • 4
  • 15