Javascript 中的 AJAX 请求在我的其余代码之前发送

无效程序

我有一个 Flask 应用程序,带有一个小的客户端 javascript。我尝试使用 AJAX 将一个对象从我的 javascript 发送到在服务器端运行的 Flask 应用程序。但是,AJAX 请求是在我的 JavaScript 代码的其余部分运行之前发送的(注意:此代码应该在发送 AJAX 请求之前运行)。上网查了一下,原来是因为AJAX默认是异步的,但是$.ajaxSetup({async: false})并没有解决我的问题。

JS代码:

function submit(){
    var no_of_subjects = parseInt(prompt("How many classes do you teach. NOTE: if you teach the same class two sujects, then consider it as 2 classes. If you are a class teacher and you teach that class a suject, then please count that class as well. (Enter a whole number)"))
    var temp = ''
    var subjects = []
    for(var i=0;i<no_of_subjects;i++){
        temp = prompt(`${i} class. Please enter the class name and section, followed by the subject you teach. For Example: 8D Biology, or 3C LifeSkills. Please leave no other spaces other than between the class name and the subject. The possible subjects can be refered in http://127.0.0.1:5000/help`)
        subjects.push(temp)
    }
    var name = document.getElementById('name').value
    var classteacher = document.getElementById('class_teacher').value
    var teacher_id = docu.getElementById('teacher_id')
    var msg = {
        "name": name,
        "class_teacher": classteacher,
        "teacher_id": teacher_id,
        "subjects": subjects
    }
    send_message(msg)
}
function send_message(msg){
    $.ajaxSetup({async: false});
    $.post("teacher_register", msg, function(){})
    event.preventDefault()
}

HTML:-

<body>
    <div class="center"><h1 class="rainbow">Register</h1></div>
    <fieldset>
        <legend>Fill in your credentials</legend>
        <form>
            Name : <input id="name" type="text"><br>
            Are you a class teacher? If you are, type the class of which you are the classteacher of. If you aren't, type false. : <input id= class_techer type="text"><br>
            Teacher id : <input id=teacher_id type="number"><br>
            <br><button onclick="submit()">Submit</button>
        </form>
    </fieldset>
</body>

烧瓶(蟒蛇)代码:-

    @app.route('/teacher_register', methods=["POST"])
    def teacher_register():
        data = request.get_json()
        app.logger.debug(data)
        return data

注意:我收到“类型错误:视图函数未返回有效响应”。该函数要么返回 None 要么在没有返回语句的情况下结束。作为代码运行时的错误。通过代码,我的意思是 Flask。这是因为数据里面没有任何价值,即它是 None

安德里亚宾达

您编写的代码不会在其余代码之前运行 $.post(..) 函数。那是不可能的。事实是,覆盖表单提交行为是一团糟。尝试更改您的代码,以便<button>在表单之外:

 <form>
            Name : <input id="name" type="text"><br>
            Are you a class teacher? If you are, type the class of which you are the classteacher of. If you aren't, type false. : <input id= class_techer type="text"><br>
            Teacher id : <input id=teacher_id type="number"><br>
            <br>
        </form>
<button onclick="submit()">Submit</button>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Javascript / Ajax中的HTTP HEAD请求?

无法从 Ajax 请求中读取 Javascript 对象

Javascript中的Ajax请求进行循环

ajax请求中的Javascript回调函数

Ajax代码在JavaScript中不起作用

在yii ajaxSubmitButton中的ajax请求之前和之后调用JavaScript函数

如何从Android应用程序中的WebView使用JavaScript发送Ajax请求

如何从Javascript / React发送AJAX请求并在Python REST API中接收数据

如何在Javascript类中通过Ajax发送对象?

ajax通过XMLHttpRequest在JavaScript中获取请求(不带表单)

如何在ajax请求中访问javascript函数

ES6 Vanilla JavaScript中的Ajax请求

在Rails 4中以“香草” javascript(无jQuery)发布AJAX请求

如何从 Django 中的 Javascript 访问 Ajax POST 请求

如何在Windows Phone 7中的每个请求之前发送预请求

为Javascript中的动态生成的元素生成多个AJAX代码,

在JavaScript代码之前触发Ajax调用

Javascript Ajax请求

$ .ajax中的javascript非法调用

CakePhp是否将时间戳添加到ajax请求的我的javascript文件中?

如何在 JavaScript 中向我的文件系统发出 AJAX 请求?

我应该在香草Django中处理Ajax请求还是在Django其余部分处理?

如何在JavaScript中访问在AJAX请求中作为响应发送的自定义Java对象?

JavaScript代码无法正确检查ajax请求

处理在IdentityServer3中的CORS之前发送的飞行前OPTIONS请求

在React Router中,如何删除/更改我之前发送的history.push中的参数?

Laravel Ajax在选择选项之前发送ID

如何在页面关闭之前发送ajax数据?

在JQuery循环中在Ajax请求之前发出警报