【问题标题】:Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file' : AngularJS SPA错误:无法在“XMLHttpRequest”上执行“发送”:无法加载“文件”:AngularJS SPA
【发布时间】:2015-06-17 21:04:39
【问题描述】:

我是 Angular SPA 的新手,我第一次通过谷歌搜索获得代码并尝试运行。但是我在控制台中遇到了一些错误。我不知道为什么,我发现了以下控制台错误列表:

XMLHttpRequest 无法加载 file:///C:/Users/hp/Downloads/single-page-app-angularjs-master/home.html。 跨域请求仅支持协议方案:http、 数据、chrome、chrome-extension、https、chrome-extension-resource。

错误:无法在“XMLHttpRequest”上执行“发送”:加载失败 'file:///C:/Users/hp/Downloads/single-page-app-angularjs-master/home.html'。 XMLHttpRequest 无法加载 file:///C:/Users/hp/Downloads/single-page-app-angularjs-master/about.html。 跨域请求仅支持协议方案:http、 数据、chrome、chrome-extension、https、chrome-extension-resource。

错误:[$compile:tpload] http://errors.angularjs.org/1.3.3/$compile/tpload?p0=home.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Single page web app using Angularjs</title>


<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
 <script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular-route.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
  <script src="script.js"></script>


</head>
<body ng-app="single-page-app">
  <div ng-controller="cfgController">
  <div>
  <nav class="navbar navbar-inverse" role="navigation" style="padding-left:130px;">
         <ul class="nav navbar-nav">
        <li class="active"><a href="#/">Home<span class="sr-only">(current)</span></a></li>
        <li><a href="#/about">About us</a></li>
        <li><a href="#/about">Read tutorial</a></li>
      </ul>
</nav>
</div>
  <br/>

  <div ng-view class="jumbotron"></div>
  </div>
</body>
</html>

【问题讨论】:

  • 您似乎遇到了 CORS 问题:en.wikipedia.org/wiki/Cross-origin_resource_sharing
  • 通常情况下,使用 file:// 时不支持 Ajax。您将希望通过 HTTP 服务器提供和访问该页面。注意:http static server one-liners
  • 我认为这个问题有很多我用来进行 ajax 调用的解决方案。一开始我不明白这个错误,但这没什么,但我没有打开我的---->>>>网络。

标签: javascript jquery angularjs single-page-application


【解决方案1】:

在 Windows 上:

chrome.exe --allow-file-access-from-files

chrome.exe --allow-file-access-from-files --disable-web-security

在 Mac 上:

open /Applications/Google\ Chrome.app/ --args --allow-file-access-from-files

【讨论】:

  • 我必须在windows哪里输入这个?
  • 我尝试在命令提示符下运行命令。它给出了这个错误--'chrome.exe' is not recognized
【解决方案2】:

我遇到了类似的事情并通过使用像Glassfish这样的应用程序服务器来解决......

换句话说,使用http:// 协议而不是file://

【讨论】:

    猜你喜欢
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 2015-12-28
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多