gpt4 book ai didi

jQuery mobile 和 JSON 发布响应

转载 作者:行者123 更新时间:2023-12-01 06:24:26 24 4
gpt4 key购买 nike

处理返回 json 响应的表单发布的最佳实践是什么?我们正在尝试在我们网站的移动版本中重用一些返回 JSON 的代码,但我不确定处理 javascript 的最佳方法。我想填充一个对话框。我真的必须在表单标签上将 data-ajax 设置为 false 并调用 $.post 吗?

谢谢,罗布

最佳答案

是的,为了在 jQuery Mobile 中处理表单提交,您必须将 data-ajax="false" 添加到表单标记,这样 jQuery Mobile 框架就不会为您处理它。然后,您可以为 submit 事件设置自己的处理程序:

//add event handler to your form's submit event
$('form').on('submit', function (e) {
var $this = $(this);

//prevent the form from submitting normally
e.preventDefault();

//show the default loading message while the $.post request is sent
$.mobile.showPageLoadingMsg();

//send $.post request to server, `$this.serialize()` adds the form data to the request
$.post($this.attr('action'), $this.serialize(), function (response) {

//you can now access the response from the server via the `response` variable
$.mobile.hidePageLoadingMsg();
}, 'json');//you can set the response data-type as well
});

这是$.post()的文档:http://api.jquery.com/jquery.post/

注意:.on() 用于代替已弃用的 .bind() 函数:http://api.jquery.com/on/

关于jQuery mobile 和 JSON 发布响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8126972/

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