0

I have a folder called DemoAPI.

In DemoAPI---> app.html

In DemoAPI--> Client\public\core.js

I am trying to include core.js in script tag of app.html.

like this:

<script type="text/javascript" src="Client/public/core.js"></script>

core.js looks like this:


 angular.module('Todo', [])

 .controller('mainController', function($scope, $http)
 {
    $scope.formData = {};

    // get all and show them
    $http.get('/musicians')
      .success(function(data) {
            $scope.todos = data;
            console.log(data);
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });

        //get with an id
        $scope.getOneTodo = function() {
        $http.get('/musicians' + id)
            .success(function(data) {
                $scope.todos = data;
                      console.log(data);
            })
            .error(function(data) {
                console.log('Error: ' + data);
            });
    };
      // send the text to the node API
    $scope.createTodo = function() {
        $http.post('/musicians', $scope.formData)
            .success(function(data) {
                $scope.formData = {}; // clear the form 
                $scope.todos = data;
                console.log(data);
            })
            .error(function(data) {
                console.log('Error: ' + data);
            });
    };

    // delete 
    $scope.deleteTodo = function(id) {
        $http.delete('/musicians' + id)
            .success(function(data) {
                $scope.todos = data;
                      console.log(data);
            })
            .error(function(data) {
                console.log('Error: ' + data);
            });
    };

    /*
    $scope.updateTodo = function(id) {
        $http.delete('/musicians' + id)
            .success(function(data) {
                $scope.todos = data;
                      console.log(data);
            })
            .error(function(data) {
                console.log('Error: ' + data);
            });
    };*/

});

But I get an error that says :

XMLHttpRequest cannot load file:///C:/musicians. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

It tried to get /musician from directory where as what I mean is the url. How do I fix this?

simi kaur
  • 1,257
  • 3
  • 15
  • 19
  • That's an error caused by something the script is trying to do, not something caused by failing to load the script. It's also an error you could get if you were loading the script from your local filesystem and not using node.js to serve it. – Quentin Jul 20 '15 at 13:58
  • @Quentin: You are right. Please see the updated question/ – simi kaur Jul 20 '15 at 13:58
  • 1
    The error message should be explanatory. You need a webserver. – Quentin Jul 20 '15 at 13:59
  • Duplicate: http://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-when-loading-a-local – Quentin Jul 20 '15 at 13:59
  • Running it does no good if you don't request your HTML document from it. – Quentin Jul 20 '15 at 14:00
  • Another duplicate: http://stackoverflow.com/questions/27742070/angularjs-error-cross-origin-requests-are-only-supported-for-protocol-schemes – Quentin Jul 20 '15 at 14:00
  • I have ndoe js installed and started the app. How else am I supposed to use the server? – simi kaur Jul 20 '15 at 14:01
  • You point your browser at `http://address.of.your.node.http.server` – Quentin Jul 20 '15 at 14:01
  • I did http-server c:/users/desktop/DemoAPI/server.js, but when I visit localhost at 8080, it downloads a file and shows nothing. – simi kaur Jul 20 '15 at 14:09
  • Well, obviously. That's what usually happens if you request a JavaScript program directly from a webserver. If you want to run client side JS then you need an HTML document to create an environment to put it in. You then put a ` – Quentin Jul 20 '15 at 14:11

1 Answers1

0

This is because XMLHTTPRequest assumes you're working in a web-based Protocol. However, it looks like you're environment is setup with OS based rules. Without a local server, you will struggle to work with files/JS. The proposed method, is to setup a quick local server with something like Python's SimpleHTTPServer module.

I usually do this by (after installing Python) navigating to the relevant folder through CLI (Command Line) and typing python -m SimpleHTTPServer 8000 and navigating to http://localhost:8000.

Bwaxxlo
  • 1,860
  • 13
  • 18