我正在尝试使用python web.py模块制作一个社交网络微型Web应用程序。 我用html制作了注册表,但是当我填写详细信息并提交时,浏览器控制台显示以下输出:
Uncaught TypeError: $(...).ready(...) is not a function
at scripty.js:22
loaded
jquery-3.4.1.min.js:2 Uncaught TypeError: Cannot read property 'init' of undefined
at HTMLDocument.<anonymous> (scripty.js:4)
at e (jquery-3.4.1.min.js:2)
at t (jquery-3.4.1.min.js:2)
我一直在寻找类似的问题,但是他们的解决方案似乎不适用于这种情况。
这是我的“ scripty.js”文件代码
$(document).ready(function () {
console.log("loaded");
$.material.init();
$(document).on("submit", "#register-form", function (e) {
e.preventDefault();
console.log("form submitted");
let form = $('#register-form').serialize(); //get the form data
//send an ajax request over to the route /postregisteration
$.ajax({
url: '/postregisteration',
type: 'POST',
data: form,
success: function (response) {
console.log(response);
}
});
});
})();
这是python中的controller.py文件代码
import web
from Models import RegisterModel
urls = (
'/', 'Home',
'/register', 'Register',
'/postregistration', 'PostRegistration'
)
render = web.template.render("Views/Templates", base="MainLayout")
app = web.application(urls, globals())
# Classes/Routes
# Each class will be controlling a route
class Home:
def GET(self):
return render.Home()
class Register:
def GET(self):
return render.Register()
class PostRegistration:
def POST(self):
data = web.input()
print(data.username)
reg_model = RegisterModel.RegisterModel.insert_user(data)
return data.username
if __name__=="__main__":
app.run()
点击提交后,它只会打印 “已加载”并在浏览器控制台上显示错误。
但它还必须显示 “表单已提交”。
任何帮助都是有意义的。
答案 0 :(得分:1)
更改已显示的JS的最后一行:
来自
})();
到
});
这可以解决您的第一个错误... is not a function
。
对于第二个错误,这意味着$.material
是undefined
。删除它(如果您不使用材料设计),或者确保相应的插件可用。
答案 1 :(得分:1)
这是您的脚本,其中修正了结束行的错误,已删除材料并添加了注释,以解释每一行的作用。
// Wait until the page has fully loaded before applying the script
$(document).ready(function () {
// Bind a function to the submit form
$(document).on("submit", "#register-form", function (e) {
// Prevent the default form post behavior to prevent the page from reloading
e.preventDefault();
// Get the form data
var form = $('#register-form').serialize();
// Send an ajax request over to the route /postregisteration
$.ajax({
url: '/postregisteration',
type: 'POST',
data: form,
success: function (response) {
console.log("form submitted");
console.log(response);
}
});
});
});