Error message 'XMLHttpRequest cannot load. Cro

2020-02-03 03:55发布

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?

7条回答
ら.Afraid
2楼-- · 2020-02-03 04:34

The following command works for me. But before the command, you need to stop chrome process anyhow, can be from task manager

 "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files --disable-web-security

Another way to make this working is as follows: At first, Open Run by clicking: Windows button + R Then, copy and paste the following command there:

 chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security

This will open the chrome browser in disabled security mode and you can overcome that Errors.

Thanks

查看更多
劳资没心,怎么记你
3楼-- · 2020-02-03 04:36

I fixed this problem by running my page off a server, like this

cd /path/to/dir/with/the/index/file
python3 -m http.server

then open http://localhost:8000 in your browser.

For other ways of serving the current directory, see this question.

查看更多
冷血范
4楼-- · 2020-02-03 04:37

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)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files --disable-web-security
查看更多
姐就是有狂的资本
5楼-- · 2020-02-03 04:37

If you're in Windows environment, and use npm for package management the easiest is to install http-server globally:

npm install -g http-server

Then simply run http-server in any of your project directories:

Eg. d:\my_project> http-server

Starting up http-server, serving ./ Available on: http:169.254.116.232:8080 http:192.168.88.1:8080 http:192.168.0.7:8080 http:127.0.0.1:8080 Hit CTRL-C to stop the server

Easy, and no security risk of accidentally leaving your browser open vulnerable.

查看更多
够拽才男人
6楼-- · 2020-02-03 04:44

You can create a virtual host with WAMP or an other web development platform.

With I have no problems.

查看更多
虎瘦雄心在
7楼-- · 2020-02-03 04:45

run a http node server and serve the file.

  1. Install connect and serve-static with NPM

    $ npm install connect serve-static

  2. Create server.js file with this content:

    var connect = require('connect'); var serveStatic = require('serve-static'); connect().use(serveStatic(__dirname)).listen(8080, function(){ console.log('Server running on 8080'); });

  3. Run with Node.js

    $ node server.js

You can now go to the file that is doing cross-origin request without any error by going: http://localhost:8080/yourfile.html

查看更多
登录 后发表回答