jQuery AJAX提交表单并输出返回值
来源:互联网 发布:illuststudio mac 编辑:程序博客网 时间:2024/04/30 17:39
前端部分:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title> </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form id="form1"> <p>Last name: <input type="text" name="name" /></p> <input id="btn"type="submit" value="Submit" /></form><script src="//jquery-2.1.3.min.js"></script> <script> $(document).ready(function(){<span style="white-space:pre"></span>$("#btn").click(function() {<span style="white-space:pre"></span>var params=$("#form1").serialize();<span style="white-space:pre"></span>$.ajax({ <span style="white-space:pre"></span>type: "post",<span style="white-space:pre"></span>data:params,<span style="white-space:pre"></span>dataType:"json",//返回值类型,建议不要默认自行定义<span style="white-space:pre"></span>//dataType: "binary", <span style="white-space:pre"></span>async: false,<span style="white-space:pre"></span>timeout: 30000, <span style="white-space:pre"></span>url: 'http://localhost/TP/index.php/Admin/Conf/viewUser', <span style="white-space:pre"></span>success: function(result){//result为接收返回值的参数名,可自行修改 <span style="white-space:pre"></span>var s = result.data;<span style="white-space:pre"></span> <span style="white-space:pre"></span>alert(s);<span style="white-space:pre"></span>},<span style="white-space:pre"></span>error: function(x, e) { <span style="white-space:pre"></span>// alert(x.readyState);<span style="white-space:pre"></span>alert(e); }<span style="white-space:pre"></span>});<span style="white-space:pre"></span>});}); </script> </body></html>此脚本将表单输入值序列化后传至后台,后台使用ThinkPHP框架,接收了POST方式传入的参数name,并将$_POST['name']的值以JSON格式打包后返回,前端已预先指定接收数据类型为JSON,并以result为返回值变量名称进行输出。
后台代码
<?phpnamespace Admin\Controller;use Think\Controller;/** * * @author XFX * user:add/change/delete,auth:add/change/delete */class ConfController extends Controller {public function Index() {$this->display('Template/Admin/Conf.html');}public function viewUser() {//$var=$_POST;$data['data'] =$_POST['name'];$this->ajaxReturn($data);}public function changeUser() {}public function delUser() {}}?>
0 0
- jQuery AJAX提交表单并输出返回值
- MVC TIP3:JQuery通过Ajax提交表单并返回结果
- JQuery -AJAX- html中提交表单并实现不跳转页面处理返回值
- jquery序列化表单并ajax提交
- jQuery+aJax表单提交
- Jquery ajax 表单提交
- jquery ajax 提交表单
- jquery ajax 提交表单
- jquery ajax 提交表单
- jQuery Ajax提交表单
- jquery ajax提交表单
- Jquery ajax 提交表单
- jQuery-AJAX提交表单
- JQuery提交Form表单,获取返回值
- (跨域)jquery.ajax提交请求并返回json数据
- jquery.ajax提交请求并返回json数据
- struts2+jquery实现ajax异步提交并返回json串
- jquery ajax表单提交插件
- iOS 检测手机是否安装SIM卡
- PCA笔记
- iOS通关书单,Android可入
- 使用AVAudioPlayer播放音频文件
- 【第四章】 资源 之 4.4 Resource通配符路径 ——跟我学spring3
- jQuery AJAX提交表单并输出返回值
- 实现医技工作站的心电图功能的代码(十九)
- 十、反射机制
- 扑克牌的结构定义
- C# 的三种序列化方法
- 在开发Ext 项目中如果遇到 Uncaught TypeError: Cannot read property 'insertAdjacentHTML' of null
- 眼前事极致了,下一步也就清楚了
- Eclipse中导入Java Project/Java Web系统,如何发布到Tomcat
- 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3