简体   繁体   English

骨干js pushState给出无法获取错误

[英]Backbone js pushState gives Cannot Get Error

Aware that there are a lot of questions on the same issue but none of them have worked. 意识到在同一问题上有很多问题,但是没有一个起作用。 Have already tried setting pushState true and hashChage false but every time the route gives me Cannot Get error. 已经尝试将pushState设置为true并将hashChage设置为false,但是每次路由给我时都无法获取错误。 Here's my routes file 这是我的路线文件

var appRouter = Backbone.Router.extend({
 routes:{
    "login":"login"
 },
 login:function () {
  var LoginView = Backbone.View.extend({
      initialize: function () {
          this.render();
      },
      render: function () {
          var that = this;
          $.get('templates/login.html', function (data) {
              template = _.template(data, {});
              that.$el.html(template);
          }, 'html');
      }
   });
   var loginView = new LoginView({ el: $("#test") });
 },
});
var sys = new appRouter();
Backbone.history.start({pushState:true});

Do you have a link in your HTML of <a href="/login"> ? 您的HTML中是否有<a href="/login">

Your browser is going to this URL. 您的浏览器将转到该URL。

You need to prevent that from happening. 您需要防止这种情况的发生。

Read this question and the top answer for a way to solve it. 阅读此问题最佳答案 ,以寻求解决方法。

This works great. 这很好。 However it throws the same error when I refresh the page or hit the URL directly. 但是,当我刷新页面或直接点击URL时,它会引发相同的错误。 Any way to solve that? 有什么办法解决吗?

Yes, but it will require you to make changes to the web server. 是的,但是这将要求您对Web服务器进行更改。

If you can add rewrite rules, you could look for URLs that don't exist and assume they are push-state generated and return your root page. 如果您可以添加重写规则,则可以查找不存在的URL,并假定它们是推送状态生成的,并返回您的根页。

Without seeing more details of your application, it's hard to say what the best solution would be. 如果不查看应用程序的更多详细信息,很难说出最佳解决方案是什么。

So as Adrian has suggested, it solves the problem for requests coming through clicks on <a> tags. 因此,正如Adrian所建议的那样,它解决了通过单击<a>标签而产生的请求的问题。 Direct Url hit and page refresh errors can be resolved by modifying your htaccess file as suggested by Adrian. 直接网址匹配和页面刷新错误可以通过按照Adrian的建议修改htaccess文件来解决。 In my case I have modified the gruntfile.js file as here 在我来说,我已经是修改了gruntfile.js文件在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM