javascript jQuery $.post $.ajax用法
时间:2024-04-16 08:52:52
jQuery.post( url, [data], [callback], [type] ) :使用POST方式来进行异步请求
参数:
url (String) : 发送请求的URL地址.
data (Map) : (可选) 要发送给服务器的数据,以 Key/value 的键值对形式表示。
callback (Function) : (可选) 载入成功时回调函数(只有当Response的返回状态是success才是调用该方法)。
type (String) : (可选)官方的说明是:Type of data to be sent。其实应该为客户端请求的类型(JSON,XML,等等)
这是一个简单的 POST 请求功能以取代复杂 $.ajax 。请求成功时可调用回调函数。如果需要在出错时执行函数,请使用 $.ajax。示例代码:
Ajax.aspx:
Response.ContentType = "application/json";Response.Write("{result: '" + Request["Name"] + ",你好!(这消息来自服务器)'}");jQuery 代码:
$.post("Ajax.aspx", { Action: "post", Name: "lulu" }, function (data, textStatus){ // data 可以是 xmlDoc, jsonObj, html, text, 等等. //this; // 这个Ajax请求的选项配置信息,请参考jQuery.get()说到的this alert(data.result); }, "json");点击提交:
这里设置了请求的格式为"json":
$.ajax()这个是jQuery 的底层 AJAX 实现。简单易用的高层实现见 $.get, $.post 等。
这里有几个Ajax事件参数:beforeSend ,success ,complete ,error 。我们可以定义这些事件来很好的处理我们的每一次的Ajax请求。
$.ajax({url: 'stat.php',
type: 'POST',
data:{Name:"keyun"},
dataType: 'html',
timeout: 1000,
error: function(){alert('Error loading PHP document');},
success: function(result){alert(result);}
});
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python+Selenium+Pytesseract实现图片验证码识别
![](https://img.aspxhome.com/file/2023/3/89043_0s.png)
Python实现图片与视频互转代码实战(亲测有效)
![](https://img.aspxhome.com/file/2023/6/118526_0s.jpg)
python进程类subprocess的一些操作方法例子
Python编程使用matplotlib绘制动态圆锥曲线示例
![](https://img.aspxhome.com/file/2023/6/85096_0s.png)
利用python中集合的唯一性实现去重
SQL Server误区30日谈 第9天 数据库文件收缩不会影响性能
对Django中内置的User模型实例详解
如何使用Python Matplotlib绘制条形图
![](https://img.aspxhome.com/file/2023/2/95852_0s.png)
Mysql下自动删除指定时间以前的记录的操作方法
Python Web版语音合成实例详解
![](https://img.aspxhome.com/file/2023/2/85262_0s.png)
WEB2.0网页制作标准教程(11)不用表格的菜单
自动更新程序的设计框架
![](https://img.aspxhome.com/file/UploadPic/20098/12/1124390-80s.jpg)
python3 拼接字符串的7种方法
使用django的objects.filter()方法匹配多个关键字的方法
php文件上传类完整实例
Go语言学习教程之反射的示例详解
![](https://img.aspxhome.com/file/2023/7/127057_0s.jpg)
Python模拟登录12306的方法
对用户研究实践的思考
![](https://img.aspxhome.com/file/UploadPic/201010/19/01-90s.jpg)
Django分页查询并返回jsons数据(中文乱码解决方法)
java正则表达式之Pattern与Matcher类详解
![](https://img.aspxhome.com/file/2023/2/58592_0s.png)