2017-08-24 110 views
1

我按住提交按鈕的同時向我的服務器發出ajax呼叫。 按鈕出現在頁面http://127.0.0.1:5000/post/15ajax呼叫的燒瓶端點

jQuery函數調用,並在Python分別submit_comment端點定義:

function submit_comment(post_id) { 
    var uname = $('input[name="uname"]').val(); 
    var comment = $('textarea[name="comment"]').val(); 

    $.ajax({ 
     url: "/submit_comment", 
     data: {name: uname, comment: comment, post_id: post_id}, 
     method: "POST", 
     datatype: 'json', 
     success: function(response) { 
      console.log('reaches here');    
      addElement(response); 
      console.log('Is it me'); 
     }, 
     error: function(error) { 
      console.log('reached Error'); 
      console.log(error); 
     } 
    }); 
} 

PY

@main.route('/submit_comment', methods = ['POST', 'PUT']) 
def submit_comment(): 
    entry = request.get_json(force=True) 
    print 'comment:', entry 
    .... 

主要是藍圖。但我得到404錯誤。

127.0.0.1 - - [24/8/2017年10時30分55秒] 「POST /後/ submit_comment HTTP/1.1」 404 -

我從那裏這篇文章得到了追加到端點疑惑。 有人可以幫助我瞭解錯誤並解決它嗎?

感謝, 迪帕克

+0

爲什麼你有一個PUT請求的例子嗎? – SD3L

回答

0

我不知道了很多,但我知道,在阿賈克斯瓶文檔說來查詢腳本根這樣的:

$SCRIPT_ROOT = {{ request.script_root|tojson }}; 

,然後使用作爲請求URL的一部分。

從我使用Ajax調用($.getJSON是一個jQuery功能$.ajax

$.getJSON($SCRIPT_ROOT + '{{ url_for("help_email") }}',