0

I'm using angularjs-seed app and I'm trying to get JSON response from server (MarkLogic) using $http. I've been on it for 3 days tried every response from other similar stackoverflow answers but not able to get it working. Please help!

The browser returns this:

XMLHttpRequest cannot load http://sreddy:8003/v1/search?format=json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. 

Here's my app code

app.js

'use strict';


// Declare app level module which depends on filters, and services
var app = angular.module('myApp', [
  'ngRoute',
  'myApp.filters',
  'myApp.services',
  'myApp.directives',
  'myApp.controllers'
]);

app.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {
  $routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
  $routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
  $routeProvider.otherwise({redirectTo: '/view1'});

  // to avoid CORS
  $httpProvider.defaults.withCredentials = true;
  $httpProvider.defaults.useXDomain = true;
  delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);

controllers.js

'use strict';

/* Controllers */

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


app.controller('MyCtrl1', ['$scope', 'dataService', function($scope, dataService) {
      $scope.test = "test";
      $scope.data = null;
      dataService.getData().then(function (dataResponse) {
          $scope.data = dataResponse;
      });
      console.log($scope.data);
  }]);

  app.controller('MyCtrl2', [function() {
      console.log("from MyCtrl2");
  }]);

services.js

'use strict';

/* Services */


// Demonstrate how to register services
// In this case it is a simple value service.
var app = angular.module('myApp.services', []);

app.value('version', '0.1');

app.service('dataService', function($http) {

this.getData = function() {
    // $http() returns a $promise that we can add handlers with .then()
    return $http({
        method: 'GET',
        url: 'http://sreddy:8003/v1/search?format=json'
     });
 }
});
Sudhakar
  • 2,904
  • 8
  • 33
  • 47
  • 4
    `No 'Access-Control-Allow-Origin' header is present on the requested resource` means that it is expecting a header from the server. Have you added this header to the server response? You cannot solve this with client side only changes. – Davin Tryon Apr 21 '14 at 22:47
  • Ah! I thought it can be handled on client side alone. Let me try again(the right way) & get back to you. Thank you sir! – Sudhakar Apr 21 '14 at 22:51
  • 1
    Added headers in server response code and got it working. Thank you Davin. – Sudhakar Apr 21 '14 at 23:11
  • This is purely a server-side config, since Angular already sends Origin header by default. – Konstantin Tarkus Apr 22 '14 at 12:40
  • @Sudhakar What headers did you add? And how did you add them? Thank you. – MauroPorras Jun 11 '14 at 14:27
  • I modified the server source code: `return if (exists($response)) then(eput:add-response-header("Access-Control-Allow-Origin", xdmp:get-request-header('Origin')), eput:add-response-header("Access-Control-Allow-Credentials","true"), $response)` I've mentioned the The same info in my github project readme as well https://github.com/csreddy/angularjs-oscars – Sudhakar Jun 12 '14 at 06:31

3 Answers3

2

useXDomain is not a thing.

You are indeed having a same-origin problem. In order for your request to work, you will need to change your headers on the request. Since you are making a simple GET request, you should be able to do it, provided it's a simple request. Your content-type is likely what is flagging the request as a CORS request.

Usually - this can be fixed by setting your content-type header to application/x-www-form-urlencoded, multipart/form-data, or text/plain, like this:

$http({
    method: 'GET',
    url: 'http://sreddy:8003/v1/search?format=json',
    headers: {'Content-Type':'text/plain'} //or whatever type
 });

Or - you can set up an interceptor and add headers to all requests meeting some criteria.

For not-simple requests, which may get preflighted, you will need to work more with the request headers in addition to ensure response headers satisfy the preflight request. If you don't have access to the server (to set response headers), you will have to work within their allowed parameters. Here is some more info on CORS.

Community
  • 1
  • 1
J.Wells
  • 1,749
  • 12
  • 13
  • I was following instructions given here http://better-inter.net/enabling-cors-in-angular-js/. Anyway its working for me now. Thank you for your response. appreciate it! – Sudhakar Apr 21 '14 at 23:46
  • It's all good. Many people have seen that blog (and others like it), promulgating this non-existent `useXDomain` feature, which actually was never in AngularJs. [Check it out](https://github.com/angular/angular.js/issues/2956#issuecomment-19493940) – J.Wells Apr 22 '14 at 01:20
  • 1
    @Sudhakar please consider accepting the answer (and possibly upvoting it) if it helped solve your issue. – Benjamin Gruenbaum May 15 '14 at 14:02
2

You need to run an http server - simplest way is using python

from the main directory of the project...

python3 -m http.server 8080

Then go to the url localhost:8080/index.html and you'r golden !

haki
  • 9,389
  • 15
  • 62
  • 110
0

I solved this issues by using express.js as a proxy server which also enables me to serve static content usinf the proxy

Ganesh
  • 301
  • 1
  • 2