角度斜杠被编码

Angular slash gets encoded

本文关键字:编码      更新时间:2023-09-26

我和这家伙有同样的问题:

angularjs-slash-after-hashbang-gets-encoded

URL 被编码为未正确路由,它似乎在我的路由配置中伪造。没有运气找到原因,我的 URL 重写工作正常。唯一的例外是当我在 HTML5 模式下在 URL 中添加哈希bang 时。我希望回退到 hashbang 并将 URL 重写为 html5 模式。

有人知道这里发生了什么吗?

更新:我将详细说明之前提供的信息:

我在服务器端使用 apache,在 .htaccess 中使用以下配置:

Options +FollowSymLinks
IndexIgnore */*
DirectoryIndex index.html index.htm
<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
</IfModule>

# html5 pushstate (history) support:
 <ifModule mod_rewrite.c>
    RewriteEngine on
        # Don't rewrite files or directories
        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^ - [L]
        # Rewrite everything else to index.html to allow html5 state links
        RewriteRule ^ index.html [L]
</ifModule>

我的$locationProvider和路由配置:

App.config(function($routeProvider, $sceDelegateProvider, $locationProvider) {
  $locationProvider.html5Mode(true);
  $locationProvider.hashPrefix('!');

  $routeProvider.when('/route1/:param', {
    templateUrl: '/html/route1.html',
    controller: 'Route1Ctrl'
  });
  $routeProvider.when('/', {
    templateUrl: '/html/route1.html',
    controller: 'Route1Ctrl'
  });
  $routeProvider.when('/route2', {
    templateUrl: '/html/route2.html',
    controller: 'Route2Ctrl'
  });
   $routeProvider.when('/route3', {
    templateUrl: '/html/route3.html',
    controller: 'Route3Ctrl'
  });
   $routeProvider.when('/route4', {
    templateUrl: '/html/route4.html',
    controller: 'Route4Ctrl'
  });
   $routeProvider.when('/route5', {
    templateUrl: '/html/route5.html',
    controller: 'Route5Ctrl'
  });
   $routeProvider.when('/route6', {
    redirectTo: '/route4'
  });

  $routeProvider.otherwise({
    redirectTo: "/"
  });

});

我终于通过两件事解决了它:

1.- 我在索引中添加了<base href="/">.html
2.- 我激活了HTML5模式但没有前缀

这样我可以使用http://localhost/#/routehttp://localhost/route,并且URL可以正确重写。