2017-05-18 25 views
0

我在一個應用程序的工作贖回,我希望能夠編輯的文檔中使用flask jquery數據的表,他們使用一種模式:如何使燒瓶路線只能通過應用

controlers:

@GCInv.route('/_add_numbers') 
def add_numbers(): 
    a = request.args.get('a', 0, type=int) 
    b = request.args.get('b', 0, type=int) 
    return jsonify(result=a + b) 

@GCInv.route('/') 
def index(): 
    return render_template('testAJAX.html') 

HTML: <

!DOCTYPE html> 
<html lang="en"> 
<head> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
    <script src="static/test.js"></script> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
<script type=text/javascript> 
    $SCRIPT_ROOT = {{ request.script_root|tojson|safe }} 
</script> 
<h1>jQuery Example</h1> 
<p><input type=text size=5 name=a> + 
    <input type=text size=5 name=b> = 
    <span id=result>?</span> 
<p><a href=# id=calculate>calculate server side</a> 
</body> 
</html> 

Java腳本:

$(function() { 
    $('a#calculate').bind('click', function() { 
     $.getJSON($SCRIPT_ROOT + '/_add_numbers', { 
     a: $('input[name="a"]').val(), 
     b: $('input[name="b"]').val() 
     }, function(data) { 
     $("#result").text(data.result); 
     }); 
     return false; 
    }); 
    }); 

在這個例子中,似乎用戶可能會調用'_add_numbers',這似乎是一個安全風險,讓這條路線開放有一種方法來製作一個裝飾器或製作路線,因此它只能在內部調用?

+0

不管名字,那你的flask.route裝飾它會使用它會被調用,你需要的地方添加路由功能 - 一種形式,一個鏈接,一些JS。這是'網絡'的方式。這不是安全問題。 – adelineu

+0

即使您創建了一個私有方法,該方法最終仍會在路由方法中被調用,但這並不意味着用戶將能夠直接調用該私有方法,因爲它沒有被定義爲路由。 – adelineu

回答

0
# not callable, because only using app.route() decorator will expose it 
def lets_do_something(): 
    # something just happened 

    return True 

@app.route('/callme') 
def callme(): 
    lets_do_something() 

    return render_template('calledit')