我正在尝试使用 python web.py 模块制作一个社交网络迷你网络应用程序。 我用 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()
点击提交后,只是打印 “已加载”并在浏览器控制台上显示错误。
但它还必须显示 “表单已提交”。
任何帮助将不胜感激。
最佳答案
更改您显示的 JS 的最后一行:
来自
})();
至
});
这修复了您的第一个错误...不是函数
。
对于第二个错误,这意味着$.material
是未定义
。删除它(如果您不使用 Material 设计)或确保相应的插件可用。
关于javascript - 未捕获的类型错误 : Cannot read property 'init' of undefined in Javascript while submitting form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56353979/