2017-05-06 92 views
1

我正在使用flask + Angular + MongoDb应用程序。我正在使用状态提供程序。状态从应用程序加载完美。但是当我刷新页面时,它正在抛出404错误。Python烧瓶角度应用程序状态不刷新

  1. App.py:(烧瓶)
application = Flask(__name__) 
    @application.route('/') 
    def showMachineList(): 
     print ("Inside the show machine list function") 
     return application.make_response(open('templates/index.html').read()) 
  • App.js(角)
  • angular.module('testapp', ['ngCookies', 'ngResource', 
         'ngSanitize', 'ui.router', 'ui.bootstrap','ngMaterial','ngCookies' 
        ]) 
        .config(function($urlRouterProvider, $locationProvider,$interpolateProvider) { 
         $urlRouterProvider.otherwise('/'); 
         //$interpolateProvider.startSymbol('//').endSymbol('//'); 
         $locationProvider.html5Mode(true).hashPrefix('!'); 
        }); 
    
  • 国家
  • angular.module('testapp') 
        .config(function ($stateProvider) { 
        console.log("inside create outage request") 
        $stateProvider 
         .state('cor', { 
         url: '/cor', 
         template: '<cor></cor>' 
         //templateUrl: '/static/client/app/acn/acn.html' 
         }); 
        }); 
    

    我已经加入index.html中的<base href="/">为好。 当我刷新页面"http://localhost:5000/cor"其投掷404. 你能让我知道我失踪了吗?

    回答

    1

    这与单页应用相当普遍的问题。看起来您的Angular应用程序可以在客户端正确处理cor状态,但您的Web服务器无法知道/cor路径应该在初始加载时路由到相同的Angular应用程序。

    尝试添加第二个路线的终点:

    @application.route('/') 
    @application.route('/cor') 
    def showMachineList(): 
        ... 
    

    或者,你可以使用catch-all route处理任何路径后缀。

    0

    刷新页面时,浏览器将URL“http://localhost:5000/cor”发送到服务器。正如我在代码中看到的那样,没有定义/cor路由,只有/。我认为你必须定义一个。

    @application.route('/cor') 
    def showMachineList(): 
        with open('templates/index.html') as fd: 
         content = fd.read() 
        return application.make_response(content) 
    

    问题可能是在您的模板:在其中定义链接cor之一。你应该有这样的事情:

    <a href="#cor">link to cor</a> 
    

    我让你写了这不是假设:

    <a href="/cor">link to cor</a> 
    
    +0

    但我已经定义了状态,我认为它也应该从Angular状态加载。 –

    +0

    您的HTML模板中可能有一个错误。 –