javascript - 在 weppy 中,我如何使用 ajax() 从 javascript 将数据发送到 python

标签 javascript python ajax

我正在尝试通过 ajax 向我的 weppy 后端发送一个 ID 号 我已经尝试了尽可能多的事情,但我确信我只是不明白该怎么做。

我通过单击从 HTML 页面收集了一个 ID,并希望将其发送到服务器脚本 现在我的ajax看起来像

$(.testclick).click(function(e){
  var idnum = e.target.id;
  ajax("{{=url('post')}}", [], ':eval');
});

我尝试在多个地方包含 idnum 变量,希望能找到答案,但没有那么幸运

post 函数看起来像

@app.route()
def post(idnum):
  idnum = idnum
  print(idnum)
  return

现在我只是想让它工作,它实际上除了打印到控制台 atm 之外什么也没做

提前致谢!

最佳答案

weppy docs起:

ajax helper asynchronously calls the url, passes the values of the field inputs with the name equal to one of the names in the list, then stores the response in the innerHTML of the tag with its id equal to target. The third argument can also be the :eval string, which leads to the evaluation via JavaScript of the string returned by the server.

如果您想使用该帮助程序对 ajax 请求使用任意值,则需要将其放入请求的 url 中,但在此之前,您需要接受 url variable在您的路线上:

@app.route('/click/<str:element_id>')
def click(element_id):
    return 'alert("clicked on ' + element_id + '");'

然后在 html 方面:

$(".testclick").click(function(e){
    ajax("{{=url('click')}}/" + e.target.id, [], ':eval');
});

请注意,weppy 提供的 ajax 帮助程序旨在用于在页面组件内呈现内容或生成要评估的 JavaScript 代码。

如果您需要向后端发送和获取任意数据,请考虑使用 jQuery ajax 方法和 json 数据。

关于javascript - 在 weppy 中,我如何使用 ajax() 从 javascript 将数据发送到 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41787090/

相关文章:

python - 我怎么能实现类似 np.where(df[variable] in ['value1' ,'value2' ])

python - 使用 to_dataframe() 作为 BigQuery 管理员角色时出现 BigQuery 权限错误

php - 在 jquery/ajax 中使用错误函数

javascript - 我可以使用 Element.closest() 在 JavaScript 中检查多个祖先吗?

javascript - 在同一页面上的 div anchor 链接在 Safari 中不起作用

javascript - 是否可以有一个继承自 Boolean.prototype 的自定义 bool 对象?

python - 如何将十六进制字符串转换为十六进制数字

javascript - 是否有在 Java 项目(不是 Node 项目)的 Git 存储库中维护第三方 javascript 库的最佳实践?

javascript - 使用 multer 接收 FormData 对象作为 node.js 服务器

javascript - 使用AJAX加载元素时重新加载JS函数