gpt4 book ai didi

javascript - 如何使用js跳页方法传递params(请求数据)?

转载 作者:行者123 更新时间:2023-11-28 04:10:26 26 4
gpt4 key购买 nike

如何使用self.location或某种方法将数据(请求)传递给views.py?

因为在我的js中,我使用self.location=/api/buy_products来跳过页面,并且在页面中,我想将数据渲染到跳过的模板。

如何处理?

我的代码如下:

在js中:

self.location = '/api/buy_product/'

在我看来.py:

def buy_product(request):

if request.method == 'GET':
return render(request, 'app/buy_products.html' , # there I want to pass the request's data)

但是如何使用 self.location 将数据传递给 buy_product 函数?

最佳答案

您也许可以使用 self.location 传递 url 参数。继续尝试一下,看看它是否有效。

JS

self.location = '/api/buy_product?data1=my_data&data2=12345';

flask

@app.route('/api/buy_product', methods=['GET'])
def buy_product(request):
if request.method == "GET":
data1 = request.args.get('data1')
data2 = request.args.get('data2')
# Whatever you want to do at this point

# If you want to render a template with the data...
return render_template('template.html', data1=data1, data2=data2)

关于javascript - 如何使用js跳页方法传递params(请求数据)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46334951/

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