2014-12-27 33 views
0

我新手在角JS,我需要通过下面的代码击溃击溃,但它得到HTTP状态404错误如何通过角JS

Web控制台显示此消息

本地主机:8080/testasd /传递addStudent无法加载资源:服务器与404状态响应(未找到)

var mainApp = angular.module("mainApp", ['ngRoute']); 

    mainApp.config(['$routeProvider', 
    function($routeProvider) { 
     $routeProvider. 
      when('/addStudent', { 
       templateUrl: 'AddStudent.html', 
       controller: 'AddStudentController' 
      }). 
      when('/viewStudents', { 
       templateUrl: 'ViewStudents.html', 
       controller: 'ViewStudentsController' 
      }). 
      otherwise({ 
       redirectTo: '/addStudent' 
      }); 
    }]); 

    mainApp.controller('AddStudentController', function($scope) { 
     $scope.message = "This page will be used to display add student form"; 
    }); 

    mainApp.controller('ViewStudentsController', function($scope) { 
     $scope.message = "This page will be used to display all the students"; 
    }); 
+0

你想它去到本地主机:8080/testasd/addStudent或localhost:8080/addStudent? –

回答

0

你需要配置你的Web服务器发送index.html要求的所有的URL。这被称为重写规则。这是因为Web浏览器将发出对该URL的GET请求,并且Web服务器希望在该位置找到文件。重写规则可以拦截该请求并将其重定向到其他文件。

您的路由中的URL在Web服务器上不存在,但需要加载AngularJS。因此,请发送来自Web根目录的index.html以获取所有请求。这假设您正在创建所谓的单一页面应用程序

对于Apache在Web根目录中创建一个名为.htaccess文件

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^(.*)$ index.html [QSA,L] 
</IfModule> 

对于IIS创建一个文件在Web根称为web.config

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
<system.webServer> 
    <rewrite> 
     <rules> 
      <clear /> 
      <rule name="angular" patternSyntax="Wildcard"> 
       <match url="*" /> 
       <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> 
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
       </conditions> 
       <action type="Rewrite" url="index.html" /> 
      </rule> 
     </rules> 
    </rewrite> 
</system.webServer> 
</configuration> 
+0

我在web根目录下做了.htaccess,但没有工作 –