2017-08-07 68 views
0

所以我有一個HTML輸入框,在點擊一個提交按鈕後調用一個javascript函數。我想將用戶在該輸入框中輸入的數據發送到我的龍捲風服務器。我對這一切都很陌生,並嘗試了幾種不同的選擇,但似乎沒有任何工作。有了這段代碼,我得到了405(方法不允許)錯誤。以下是我現在所擁有的:如何使用JSON將字符串發送到我的服務器? (龍捲風)

var myData = hello 

function pushURL(){ 

    var passThis = { 
     apples : myData 
    } 

    $.ajax({ 
     url: "/", 
     type: 'POST', 
     contenttype: 'application/json; charset=utf-8', 
     data : JSON.stringify(passThis), 
     dataType: 'JSON' 
    }); 
} 

,這裏是我的龍捲風腳本:

import tornado.ioloop 
import tornado.web 
import json 

#Utility libraries 
import os.path 

class MainHandler(tornado.web.RequestHandler): 
    def get(self): 
     self.render('index.html') 

#This tells tornado where to find static files 
settings = dict(
    template_path = os.path.join(os.path.dirname(__file__), "templates"), 
    static_path = os.path.join(os.path.dirname(__file__), "static"), 
    debug = True 
) 

# r"/" == root website address 
application = tornado.web.Application([ 
    (r"/", MainHandler) 
],**settings) 

#Start the server at port n 
if __name__ == "__main__": 
    print('Server Running...') 
    print('Press ctrl + c to close') 
    application.listen(8888) 
    tornado.ioloop.IOLoop.instance().start() 

我將需要使用字符串中的一些服務器端Python腳本,但現在我只想看到它在用戶點擊提交時打印到控制檯。任何幫助將不勝感激。另外,如果你可以用ELI5的條款解釋,那很酷。

回答

0

您正在將數據作爲發送請求從前端發送到您的索引路由,因此您必須在MainHandler類中定義post方法。 請參閱本文檔here