Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

javascript - Error message 'XMLHttpRequest cannot load. Cross origin requests are only supported for HTTP in AngularJS'

This is my code:

MyAppModule.factory('EventData', function($http,$log){
    return {
        getEvent : function(successcb){

            $http({method: 'GET', url: './js/Services/products.json'}).

            success(function(data) {
                $log.info("success");
            }).
            error(function(data) {
                $log.info("error");
            });
        }
    };
});

I have a simple JSON file in a local location, and I am trying to read it using the http method of AngularJS. I am getting the following error:

XMLHttpRequest cannot load file:///C:/Users/Avraam/Documents/GitHub/AngularJS/app/js/Services/products.json Cross origin requests are only supported for HTTP. angular.min.js:73 Error: A network error occurred.

What is my mistake? I am not using any server; I am just openning my index file with Chrome. Is this the mistake? Should I use a server if I want to use the http method?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If this is for local development and you are using Chrome, you need to run Chrome with a couple of arguments to relax security like this:

"C:Program Files (x86)GoogleChromeApplicationchrome.exe" --allow-file-access-from-files --disable-web-security

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...