错误消息“XMLHttpRequest 无法加载.跨源请求仅支持 AngularJS 中的 HTTP' [英] Error message 'XMLHttpRequest cannot load. Cross origin requests are only supported for HTTP in AngularJS'

查看:28
本文介绍了错误消息“XMLHttpRequest 无法加载.跨源请求仅支持 AngularJS 中的 HTTP'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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");
            });
        }
    };
});

我在本地位置有一个简单的 JSON 文件,我正在尝试使用 AngularJS.我收到以下错误:

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 无法加载 file:///C:/Users/Avraam/Documents/GitHub/AngularJS/app/js/Services/products.json 跨源请求仅支持 HTTP.angular.min.js:73错误:发生网络错误.

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.

我的错误是什么?我没有使用任何服务器;我只是用 Chrome 打开我的索引文件.这是错误吗?如果我想使用 http 方法,我应该使用服务器吗?

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?

推荐答案

如果这是用于本地开发并且您使用的是 Chrome,则需要使用几个参数来运行 Chrome 以放松安全性,如下所示:

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

这篇关于错误消息“XMLHttpRequest 无法加载.跨源请求仅支持 AngularJS 中的 HTTP'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆