角UI路由器pressing刷新导致404错误 [英] Angular ui-router pressing refresh causes 404 error

查看:124
本文介绍了角UI路由器pressing刷新导致404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道这是一个开放式的问题,但 -

OK, I know this is an open ended question, but -

我正在使用的1.4.x AngularJS和UI路由器的应用程序。在大多数情况下都工作正常,符合预期。我的各种页面中使用的UI SREF的浏览,页面显示为预期,且该网址显示的正确外观。但是,如果我刷新页面(F5)在任何网页上的话,那会导致发生404错误。并键入URL中不再起作用。

I'm running an application using AngularJS 1.4.x and ui-router. For the most part everything works fine and as expected. My various pages use ui-sref's to navigate, pages show up as expected, and the URL that's showing looks correct. However, if I refresh the page (F5) on any page at all, it causes a 404 error to occur. And typing in the URL no longer works.

这可能涉及到的东西:


  • 我打开 $ locationProvider.html5Mode(真)标志

  • 我用

  • I turned on the $locationProvider.html5Mode(true) flag
  • I use

$ rootScope。在('$ stateChangeStart'$,功能(事件,toState,
   toParams)

$rootScope.$on('$stateChangeStart', function (event, toState, toParams)

管理页面访问(认证)

推荐答案

问:当您刷新页面,什么是真正发生的事情

Q: When you refresh the page, what is actually happening?

答:您可以发送通过浏览器到服务器的HTTP请求。 GET /不管。因此服务器需要有设立这一要求与响应的index.html 的路线。目前,你得到 404 ,因为您的服务器不具有请求 GET /匹配的路由无论

A: You're sending an HTTP request via your browser to a server. GET /whatever. So the server will need to have a route set up for this request that responds with index.html. Currently, you're getting a 404 because your server doesn't have a route that matches the request to GET /whatever.

问:为什么?

答:当你通过浏览器发出请求(而不是通过AJAX),您的浏览器将尝试向您展示的响应。防爆。如果响应是JSON,它会告诉你JSON。如果它是一个字符串,它会告诉你一个字符串。如果它是一个HTML页面,它会告诉你这一点。你不能只是发回任何路线的模板,因为如果你这样做,它会只呈现,没有其他方面的模板。您需要 index.html的加载角,运行控制,将模板在它的用户界面视图容器等

A: When you make a request via your browser (rather than through AJAX), your browser will try to display the response to you. Ex. if the response is JSON, it'll show you JSON. If it's a string, it'll show you a string. If it's an html page, it'll show you that. You can't just send back a template for whatever route, because if you do, it'll just render that template with no other context. You need index.html to load angular, run your controller, place the template in it's ui-view container, etc.

(会发生什么事是 index.html的将被送回,UI路由器会检查路线,作出了 templateUrl <请求/ code>,运行控制,然后渲染它。)

(What'll happen is index.html will be sent back, UI Router will check the route, make a request for the templateUrl, run the controller, and then render it.)

Q:如何

答:这取决于)您正在使用和b)你的文件结构的服务器类型。你可能需要某种渔获物在你的路线文件的末尾,供应 index.html的所有路线。这就是我如何与节点/ EX preSS做到这一点:

A: That depends on a) what type of server you're using and b) your file structure. You'll probably want some sort of catch all route at the end of your route file that serves index.html. This is how I do it with Node/Express:

app.get('/*', function(req, res) {
  var url = path.resolve(__dirname + '/../client/index.html');
  res.sendFile(url, null, function(err) {
    if (err) res.status(500).send(err);
    else res.status(200).end();
  });
});

这篇关于角UI路由器pressing刷新导致404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆