$.ajax相关用法
$.ajax({ type: "GET" , url: "Services/EFService.svc/Members" , data: "{}" , contentType: "application/json; charset=utf-8" , dataType: "json" , success: function (data) { // Play with returned data in JSON format }, error: function (msg) { alert(msg); } }); |
全局事件是每次的Ajax请求都会触发的,它会向DOM中的所有元素广播,在上面 getScript() 示例中加载的脚本就是全局Ajax事件。全局事件可以如下定义: $( "#loading" ).ajaxStart( function (){ $( this ).show(); }); 我们可以在特定的请求将全局事件禁用,只要设置下 global 选项就可以了: $.ajax({ url: "test.html" , global: false , // 禁用全局Ajax事件. // ... }); 下面是jQuery官方给出的完整的Ajax事件列表: •ajaxStart (Global Event) This event is broadcast if an Ajax request is started and no other Ajax requests are currently running. •beforeSend (Local Event) This event, which is triggered before an Ajax request is started, allows you to modify the XMLHttpRequest object (setting additional headers, if need be.) •ajaxSend (Global Event) This global event is also triggered before the request is run. •success (Local Event) This event is only called if the request was successful (no errors from the server, no errors with the data). •ajaxSuccess (Global Event) This event is also only called if the request was successful. •error (Local Event) This event is only called if an error occurred with the request (you can never have both an error and a success callback with a request). •ajaxError (Global Event) This global event behaves the same as the local error event. •complete (Local Event) This event is called regardless of if the request was successful, or not. You will always receive a complete callback, even for synchronous requests. •ajaxComplete (Global Event) This event behaves the same as the complete event and will be triggered every time an Ajax request finishes. •ajaxStop (Global Event) This global event is triggered if there are no more Ajax requests being processed. jQuery.ajaxSetup( options ) : 设置全局 AJAX 默认选项。 设置 AJAX 请求默认地址为 "/xmlhttp/" ,禁止触发全局 AJAX 事件,用 POST 代替默认 GET 方法。其后的 AJAX 请求不再设置任何选项参数。 jQuery 代码: $.ajaxSetup({ url: "/xmlhttp/" , global: false , type: "POST" }); $.ajax({ data: myData }); |
如果指定为 html 类型,任何内嵌的 JavaScript 都会在 HTML 作为一个字符串返回之前执行。类似地,指定 script 类型的话,也会先执行服务器端生成 JavaScript,然后再把脚本作为一个文本数据返回。
JSON 数据是一种能很方便通过 JavaScript 解析的结构化数据。如果获取的数据文件存放在远程服务器上(域名不同,也就是跨域获取数据),则需要使用 jsonp 类型。使用这种类型的话,会创建一个查询字符串参数 callback=? ,这个参数会加在请求的 URL 后面。服务器端应当在 JSON 数据前加上回调函数名,以便完成一个有效的 JSONP 请求。如果要指定回调函数的参数名来取代默认的 callback,可以通过设置 $.ajax() 的 jsonp 参数。
随机推荐
- Java中的字符串问题
本文章分为三个部分: 1.创建字符串对象的两种方式以及它们的存储方式 2.String a = new String("a")创建了几个对象的问题 3.字符串小例子 ------- ...
- SAP Netweaver的负载均衡消息服务器 vs CloudFoundry的App Router
Message server for ABAP Netweaver SAP传统应用经典的三层架构: 起到负载均衡的消息服务器(Message Server)在图中没有得到体现.然后,消息服务器在我们每 ...
- Android(java)学习笔记76:Handler用法总结 和 秒表案例
一.Handler的定义: Handler主要接收子线程发送的数据, 并用此数据配合主线程更新UI,用来跟UI主线程交互用.比如可以用handler发送一个message,然后在handler的线程中 ...
- Android(java)学习笔记91:Eclipse中代码提示去掉@override,不然就报错!
1. Eclipse中提示去掉@Override 把项目下载下来后有@Override的注释的方法会报错,如果把@Override去掉就不报错了.经过查阅后发现:@override注释在jdk1.5环 ...
- hashlib模块常用功能
什么是hash hash是一种算法,该算法接受传入的内容,经过运算得到一串hash值 如果把hash算法比喻为一座工厂 那传给hash算法的内容就是原材料 生成的hash值就是生产出的产品 2.为何要 ...
- PHP实现的敏感词过滤方法
PHP实现的敏感词过滤方法,以下是一份过滤敏感词的编码.有需要可以参考参考. /** * @todo 敏感词过滤,返回结果 * @param array $list 定义敏感词一维数组 * @para ...
- linux 命令学习(持续完善中...)
linux 命令学习(持续完善中...) 主要是记录一些开发过程中用到的linux命令,慢慢补充 一.用户 1.添加用户: useradd 用户名 2.设置密码:passwd 用户名 ,然后按照提示输 ...
- JZOJ 5347. 遥远的金字塔
Description Input Output Sample Input 5 3 1 6 1 5 3 5 4 4 4 4 Sample Output 15 Data Constraint 做法: 其 ...
- 本地Navicat连接虚拟机MySQL
安装完MySQL后,使用mysql命令进去,然后执行以下命令 grant all privileges on hive_metadata.* to 'hive'@'%' identified by ' ...
- VMWare workstation Pro 14 For Linux key
VMWare workstation Pro 14 For Linux key: (我使用的Linux 系统是 Ubuntu16.04, 64位 ) 镜像是官方网址下载的,你也可以自己去官方网址下载: ...