即使部署到服务器上,AngularJS应用程序也不能读取本地文本文件

AngularJS application cant read local text file even after deployed on server

本文关键字:读取 也不能 文件 文本 应用程序 AngularJS 部署 服务器      更新时间:2023-09-26

我的应用程序中有4个文件(在同一目录中),部署在IIS上-

  1. home。
  2. Angular.js
  3. data.txt
  4. 网络。配置(默认文件由IIS自动生成)

我想在'home.html'上显示'data.txt'的内容。相同的代码运行在[Plunker][1]

这个'home.html'文件的代码如下:

<!DOCTYPE html>
  <html>
 <head>
<title>nitin</title>
<script src="angular.js" type="text/javascript"></script>
<script type="text/javascript">
    var nameSpace = angular.module("app", []);
    nameSpace.controller("GuitarFunction", function ($scope) {});
  </script>
 </head>
 <body ng-app="app">
 <div ng-controller="GuitarFunction">     
    <ng-include src="'data.txt'">
    </ng-include>
 </div>
</body>
</html>

http://plnkr.co/edit/8aBJsqzV07AweDk4PAcG?p =预览

网站需要托管在一个web服务器(IIS, Apache),所以它可以通过http/https提供。

浏览器安全措施将阻止脚本加载外部资源,如果协议是file://。

如果您在IIS中托管C:'webapp,例如端口5000,您需要将浏览器定向到http://localhost:5000/index.html,而不是C:'webapp'index.html.

您需要在web中包含覆盖。如果你的网站没有服务器代码,那么你的覆盖只是应用程序的名称,如

<?xml version="1.0" encoding="utf-8"?>
 <configuration>
<system.webServer>
  <handlers>
       <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
  </handlers>
  <rewrite>
       <rules>
            <rule name="DynamicContent">
                 <conditions>
                      <add input="{{REQUEST_FILENAME}}" matchType="IsFile" negate="True"/>
                 </conditions>
                 <action type="Rewrite" url="server.js"/>
            </rule>
       </rules>
  </rewrite>     

相关文章: