gpt4 book ai didi

javascript - Flask GET 请求未加载页面

转载 作者:行者123 更新时间:2023-12-02 21:19:45 24 4
gpt4 key购买 nike

晚上好。我试图阻止用户使用 reCaptcha v3 访问我的网站。但是,尽管我的调试日志显示发送了 GET 请求并且找到了页面,但执行此操作的模板并未加载:

routes.py 的相关位

from flask import url_for, render_template, redirect, request, render_template_string
from flask import current_app as app
from .forms import *
from .filewriter import *
from .mailserver import *
from .rcvalidator import *
from flask_jsglue import JSGlue

jsglue = JSGlue(app)

@app.route('/', methods=('GET', 'POST'))
def home():
reqform = ReqForm()
delform = DelForm()
error = 0
if request.method == "POST":
gtoken = request.json['gtoken']
if not rc_form_valid(gtoken):
print("working")
return redirect(url_for('inv'))
return render_template('home.html',
reqform=reqform,
delform=delform,
csrf_token=0,
errors=error)

@app.route('/invalid')
def inv():
print("loaded inv")
return render_template_string("get out now")

“working”和“loaded inv”都会被打印。在我的layout.py中,向服务器发送了一个ajax JSON post以提供RC token :

layout.html

<!DOCTYPE html>

<html lang="en">

<head>
{{ JSGlue.include() }}
{% include 'meta.html' %}
{% block styles %}{% endblock %}
<script src="https://kit.fontawesome.com/e3deaeba31.js" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js?render=6LeRCuMUAAAAAEWkxJ1IsbM4h-RQbGuxDxCXmdtr"></script>
<link href="https://fonts.googleapis.com/css?family=Poppins:200,300,500" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script>

var csrftoken = $('meta[name=csrf-token]').attr('content')

$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type)) {
xhr.setRequestHeader("X-CSRFToken", csrftoken)
}
}
})

grecaptcha.ready(function() {
grecaptcha.execute('secret token is here', {action: 'homepage'})
.then(function(token) {
//appending the token to the end of the form so that the thing can go in and submit the token in the background and find the number
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: Flask.url_for("home"),
data: JSON.stringify({"gtoken":token}),
})
});
});
</script>
</head>

<body class="{{template}}">
{% block content %}{% endblock %}
{% block javascript %}{% endblock javascript %}
</body>

</html>

meta.html提供了相关变量。

日志:

 * Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 502-140-539
127.0.0.1 - - [26/Mar/2020 16:21:02] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [26/Mar/2020 16:21:02] "GET /jsglue.js HTTP/1.1" 200 -
{'success': True, 'action': 'homepage', 'hostname': 'localhost', 'score': 0.9, 'challenge_ts': 'xxxxx'}
working
127.0.0.1 - - [26/Mar/2020 16:21:03] "POST / HTTP/1.1" 302 -
loaded inv
127.0.0.1 - - [26/Mar/2020 16:21:03] "GET /invalid HTTP/1.1" 200 -

日志到此结束,页面相同。请帮忙

最佳答案

我在很长一段时间后才设法让它工作。由于 AJAX 的异步特性,在进行 AJAX 调用后不会加载重定向。您需要做的就是在 AJAX JSON 帖子中添加成功行,其中显示

成功:函数(响应){window.location.href = response.redirect}

然后,在 Flask 中,当您在函数中处理请求时,只需返回带有重定向属性的 JSON:

return jsonify({'redirect': url_for("success")})

关于javascript - Flask GET 请求未加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60877789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com