$.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 参数。
随机推荐
- Q9400为何难以100%全速运行
采用基于正域的约简. 数据:Ticdata2000 记录数:5822 条件属性:85 结果: 1. Core i7 3632QM 4四核八线程 2.2G 动态加速3.2G 0.516s 2. Core ...
- JS事件阻止冒泡的写法
$("body").on("click", "#id", function (ev) { ev = ev || event;要写的逻辑代码 ...
- Shuffle Cards
C: Shuffle Cards 时间限制: 1 Sec 内存限制: 128 MB提交: 3 解决: 3[提交] [状态] [讨论版] [命题人:admin] 题目描述 Eddy likes to ...
- 2017.12.11 String 类中常用的方法
1.编写程序将 "jdk" 全部变为大写,并输出到屏幕,截取子串"DK" 并输出到屏幕 package demo; import java.util.Scann ...
- python读取文件指定行
import linecache file=open('3_2.txt','r') linecount=len(file.readlines()) linecache.getline('3_2.txt ...
- Window_Bat_Scripts—检测特定网段未使用的IP地址
1.1 脚本名称 Check_IP_Not_Use.bat 1.2 脚本代码 @Echo off set /p input_number=请输入网络位(192.168.1.): IF EX ...
- 配置vim nginx.conf高亮
#!/bin/bashwget http://www.vim.org/scripts/download_script.php?src_id=14376 -O nginx.vimmv nginx.vim ...
- 【Ecshop】后台菜单与权限管理
主要php文件: 1,admin/includes/inc_menu.php ECSHOP管理中心菜单数组--配置菜单组及URL 2,languages/zh_cn/admin/common.php ...
- 26.VUE学习之--提交表单不刷新页面,事件修饰符之使用$event与prevent修复符操作表单
提交表单不刷新页面 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- python字符编码小结
首先简要说一下各种字符编码: 1. ASCII 计算机只认识0101,但如何让计算机认识人类语言?将每个字母和符号给予固定的编号,然后将这个编号转换成二进制,计算机就可以正确识别这些字母与符号,同时计 ...