3

I'm learning the MEAN stack but for now have not started with the db. I tried to wire the rest of the stack but it throws an error

Error: Access to restricted URI denied createHttpBackend

I googled and could not find any solutions to this problem.

This is my main controller in Angular app.js:

app.controller('MainCtrl', function($scope, $http) {
    console.log("success");
    $http.get('/').
    success(function(data, status, headers, config) {
        console.log("success");
        $scope.name = data.name;
    }).
    error(function(data, status, headers, config) {
        $scope.name = 'Error!';
    })
});

My node server app.js:

var express = require('express'),
    http = require('http');

var app = express();

app.set('port', 3000);

app.get('/', function(req, res) {
    var name = 'MyNameFromServer';
    res.send(name);
})

http.createServer(app).listen(app.get('port'), function () {
  console.log('Express server listening on port ' + app.get('port'));
});

And this is the error in browser console ( when Angular http.get happens):

"Error: Access to restricted URI denied
createHttpBackend/<@file:///F:/routing/js/angular.js:9902:6
sendReq@file:///F:/routing/js/angular.js:9703:0
$http/serverRequest@file:///F:/routing/js/angular.js:9415:15

Please help me out.

This is the full app - https://github.com/V1cHu/routing

14
  • try to some other url instead of "/" Commented Jun 23, 2015 at 16:01
  • Which part of the stack is throwing this error? I can't tell if it's the node app throwing this in bash or angular in the browser console Commented Jun 23, 2015 at 16:08
  • angular throws in browser console. Commented Jun 23, 2015 at 16:09
  • @Mohit acutally when I try to hit the link localhost:3000/ , node console shows the GET hit , but when I try this using the angular app , it does not hit the node server at all Commented Jun 23, 2015 at 16:10
  • Try $http.get('http://localhost:3000/') instead of $http.get('/') Commented Jun 23, 2015 at 16:10

1 Answer 1

2

After reviewing your code,I found there are few problems in your code 1) You don't need to add that crossbrowser support code

app.all('/*', function(req, res, next) {
 res.header("Access-Control-Allow-Origin", "*");
 res.header("Access-Control-Allow-Headers", "X-Requested-With");
 res.header("Access-Control-Allow-Methods", "GET, POST","PUT");
 next()}
);

and also this code

$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];

2) In the server side app.js file change the call app.get('/') to some other route for eg app.get('/xyz') because it does not allow you to load your angular js code

3)In your main controller (MainCtrl) use $http.get('/xyz') as updated above.Don't use $http.get('http://localhost:3000/')

$http.get('/xyz').
success(function(data, status, headers, config) {
    console.log("success");
    $scope.name = data;
}).
error(function(data, status, headers, config) {
    $scope.name = 'Error!';
})

EDIT: in server side app.js add this code

var path = require('path')
app.use(express.static(path.normalize(__dirname + '/')));

and in the client side app.js file change $scope.name to data not data.name

  $scope.name = data;

It will solve issues in your problem

Sign up to request clarification or add additional context in comments.

12 Comments

I tried each and every step of yours. It still doesn't work. I get "Access to restricted URI error" now.
I have checked your code on my machine.It is working fine
Please run this code at this link.This has been forked from your repo and works perfectly on my machine
I ran your code, still it does not work for me. It throws the same error. Please let me know how exactly do you run the code ? I start the node server and then open index.html in my browser.
go into your directory and type "node app" in the command prompt.and then run localhost:3000 in the browser.You don't need to open index.html by your self
|

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.