Reloading the page gives wrong GET request with AngularJS HTML5 mode

前端 未结 24 2961
慢半拍i
慢半拍i 2020-11-22 01:39

I want to enable HTML5 mode for my app. I have put the following code for the configuration, as shown here:

return app.config([\'$routeProvider\',\'$location         


        
相关标签:
24条回答
  • 2020-11-22 02:00

    I have this simple solution I have been using and its works.

    In App/Exceptions/Handler.php

    Add this at top:

    use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
    

    Then inside the render method

    public function render($request, Exception $exception)
    {
        .......
    
           if ($exception instanceof NotFoundHttpException){
    
            $segment = $request->segments();
    
            //eg. http://site.dev/member/profile
            //module => member
            // view => member.index
            //where member.index is the root of your angular app could be anything :)
            if(head($segment) != 'api' && $module = $segment[0]){
                return response(view("$module.index"), 404);
            }
    
            return response()->fail('not_found', $exception->getCode());
    
        }
        .......
    
         return parent::render($request, $exception);
    }
    
    0 讨论(0)
  • 2020-11-22 02:01

    Your server side code is JAVA then Follow this below steps

    step 1 : Download urlrewritefilter JAR Click Here and save to build path WEB-INF/lib

    step 2 : Enable HTML5 Mode $locationProvider.html5Mode(true);

    step 3 : set base URL <base href="/example.com/"/>

    step 4 : copy and paste to your WEB.XML

     <filter>
         <filter-name>UrlRewriteFilter</filter-name>
     <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    

    step 5 : create file in WEN-INF/urlrewrite.xml

     <urlrewrite default-match-type="wildcard">
    
    
        <rule>
                <from>/</from>
                <to>/index.html</to>
            </rule>
    
        <!--Write every state dependent on your project url-->
        <rule>
                <from>/example</from>
                <to>/index.html</to>
            </rule>
        </urlrewrite>
    
    0 讨论(0)
  • 2020-11-22 02:03

    Just write out a rule in web.config

    <system.webServer>
      <rewrite>
        <rules>
        <rule name="AngularJS Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
           </rules>
        </rewrite>
    </system.webServer>
    

    In the index add this

    <base href="/">
    

    it works for me maybe you've forgotten to set Html5Mode app.config

    app.config(function ($stateProvider, $urlRouterProvider, $locationProvider){
        $locationProvider.html5Mode({ enabled: true, requireBase: true });
        $locationProvider.hashPrefix('!');
    }
    
    0 讨论(0)
  • 2020-11-22 02:05

    I have resolved the issue by adding below code snippet into node.js file.

    app.get("/*", function (request, response) {
        console.log('Unknown API called');
        response.redirect('/#' + request.url);
    });
    

    Note : when we refresh the page, it will look for the API instead of Angular page (Because of no # tag in URL.) . Using the above code, I am redirecting to the url with #

    0 讨论(0)
  • 2020-11-22 02:06

    You need to configure your server to rewrite everything to index.html to load the app:

    https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#wiki-how-to-configure-your-server-to-work-with-html5mode

    0 讨论(0)
  • 2020-11-22 02:06

    I solved to

    test: {
            options: {
              port: 9000,
              base: [
                '.tmp',
                'test',
                '<%= yeoman.app %>'
              ],
             middleware: function (connect) {
                      return [
                          modRewrite(['^[^\\.]*$ /index.html [L]']),
                          connect.static('.tmp'),
                          connect().use(
                              '/bower_components',
                              connect.static('./bower_components')
                          ),
                          connect.static('app')
                      ];
                  }
            }
          },
    
    0 讨论(0)
提交回复
热议问题