【问题标题】:AngularJS page refresh problemsAngularJS页面刷新问题
【发布时间】:2017-06-08 06:30:51
【问题描述】:

在使用 Angular 及其路由时,如果您在 localhost:9000/products 中重新加载页面,则响应将是 404。

我正在使用使用python -m SimpleHTTPServer port no 创建的 Python 服务器。由于 .htaccess 文件在此不起作用,如何解决此问题?

【问题讨论】:

  • 您是否启用了 Html5 模式?如果是,则添加<base href="/">

标签: angularjs .htaccess


【解决方案1】:

.htaccess 文件用于 apache http 服务器而不是 python 服务器,.htaccess 用于设置 apache 服务器将观察到的重定向,但如果您使用 nginx 或在这种情况下为 python 简单 http 服务器,您必须使用重定向特定于特定的 http 服务器,这可能会有所帮助:

https://gist.github.com/chrisbolin/2e90bc492270802d00a6

这里复制的不是我自己写的,显然也是从 SO

''' Taken from: http://stackoverflow.com/users/1074592/fakerainbrigand http://stackoverflow.com/questions/15401815/python-simplehttpserver '''

import SimpleHTTPServer, SocketServer import urlparse, os

PORT = 3000 INDEXFILE = 'index.html'

class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):    def do_GET(self):

    # Parse query data to find out what was requested
    parsedParams = urlparse.urlparse(self.path)

     # See if the file requested exists
    if os.access('.' + os.sep + parsedParams.path, os.R_OK):
        # File exists, serve it up
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self);
    else:
        # send index.html, but don't redirect
        self.send_response(200)
        self.send_header('Content-Type', 'text/html')  
        self.end_headers()
        with open(INDEXFILE, 'r') as fin:
          self.copyfile(fin, self.wfile)

Handler = MyHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT httpd.serve_forever()

另外,我个人在本地使用 apache,只有浏览器同步代理到 apache 服务器,如果找不到文件,它会处理重定向,从那里角度页面接管并启动路由以恢复视图或转到页面未找到视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2018-07-13
    • 2011-05-13
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多