如何在fetchapi命令中添加局部变量

2024-09-23 22:25:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在fetch命令中传递这些参数,以便将这些信息保存在数据库中


function addItemToCart(title,price,imgSrc)
{
    console.log('starting ajax call')
                var requestOptions = {
                    method: 'GET',
                    redirect: 'follow'
                  };

                  fetch("http://127.0.0.1:5000/query?product=title", requestOptions)
                    .then(response => response.text())
                    .then(result => console.log(result))
                    .catch(error => console.log('error', error));
}

Tags: 命令log信息数据库参数titleresponsefunction
1条回答
网友
1楼 · 发布于 2024-09-23 22:25:40

如果在变量和字符串之间插入+,则可以向字符串添加变量。所以你不会这么做:

var url = 'http://127.0.0.1:5000/query?product=' + title;
var requestOptions = {
                    method: 'GET',
                    redirect: 'follow'
                  };
fetch(url, requestOptions)
                    .then(response => response.text())
                    .then(result => console.log(result))
                    .catch(error => console.log('error', error));

然后,您可以使用以下代码行解析结果:

# Top
from flask import request

# View code
title = request.args.get('product')

希望这有帮助

请记住,在执行args.get时,字符串应该是url中的“变量”,因此如果要查找id,则应该说www.url.com?id=15,但如果需要,可以使用任何单词代替id

相关问题 更多 >