合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# jquery中$.ajax()方法使用详解 **1.url** 说明:发送请求的地址(默认为当前页面),要求是String类型的参数,比如是.net下,"~wexin(控制器)/getweinxinmenu(动作)", **2.type** 说明:请求方法(post或者get),默认是get,要求是String类型的参数。其他的htpp请求如(put、delete)等也支持,但是要浏览器支持 **3.timeout** 说明:设置请求超时时间(毫秒),要求是number类型的参数。此设置将覆盖$.ajaxSetup()方法的全局设置。 **4.async** 说明:默认值为true,所有请求是异步,要求是Bool类型的参数,如果需要发送同步请求,将此项设置为false。注意:同步时浏览器会被锁住。 **5.cache** 说明:默认为true(当datatype为script时,默认为false)从浏览器缓存中加载请求信息,要求是Bool类型的参数。 **6.data** 说明:发送到服务器的数据,要求是Object或string类型的参数。如果已经不是字符串,将自动转换为字符串格式。get请求中将附加在url后。防止这种自动转换,可以查看 processData选项。对象必须为key/value格式,例如{foo1:"bar1",foo2:"bar2"}转换为&foo1=bar1&foo2=bar2。如果是数组,JQuery将自动为不同值对应同一个名称。例如{foo:\["bar1","bar2"\]}转换为&foo=bar1&foo=bar2。 7.**dataType**: 说明:预期服务器返回的数据类型,要求为String类型的参数。如果不指定,JQuery将自动根据http包mime信息返回responseXML或responseText,并作为回调函数参数传递。可用的类型如下: xml:返回XML文档,可用JQuery处理。 html:返回纯文本HTML信息;包含的script标签会在插入DOM时执行。 script:返回纯文本JavaScript代码。不会自动缓存结果。除非设置了cache参数。注意在远程请求时(不在同一个域下),所有post请求都将转为get请求。 json:返回JSON数据。 jsonp:JSONP格式。使用SONP形式调用函数时,例如myurl?callback=?,JQuery将自动替换后一个“?”为正确的函数名,以执行回调函数。 text:返回纯文本字符串。 **8.beforeSend**: 说明:发送请求前可以修改XMLHttpRequest对象的函数(也可以修改页面一下信息),例如添加自定义HTTP头。要求为Function类型的参数。在beforeSend中如果返回false可以取消本次ajax请求。XMLHttpRequest对象是惟一的参数。 function(XMLHttpRequest){ this; //调用本次ajax请求时传递的options参数 } **9.complete**: 说明:请求完成后调用的回调函数(请求成功或失败时均调用)。要求为Function类型的参数,参数:XMLHttpRequest对象和一个描述成功请求类型的字符串。 function(XMLHttpRequest, textStatus){ this; //调用本次ajax请求时传递的options参数 } **10.success**: 说明:请求成功后调用的回调函数,要求为Function类型的参数,有两个参数。 (1)由服务器返回,并根据dataType参数进行处理后的数据。 (2)描述状态的字符串。 function(data, textStatus){ //data可能是xmlDoc、jsonObj、html、text等等 this; //调用本次ajax请求时传递的options参数 } **11.error**: 说明:请求失败时被调用的函数。要求为Function类型的参数,该函数有3个参数,即XMLHttpRequest对象、错误信息、捕获的错误对象(可选)。ajax事件函数如下: function(XMLHttpRequest, textStatus, errorThrown){ //通常情况下textStatus和errorThrown只有其中一个包含信息 this; //调用本次ajax请求时传递的options参数 } **12.contentType**: 说明:内容编码类型。要求为String类型的参数,当发送信息至服务器时,内容编码类型默认为"application/x-www-form-urlencoded"。该默认值适合大多数应用场合。 **13.dataFilter**: 说明:给Ajax返回的原始数据进行预处理的函数。要求为Function类型的参数,提供data和type两个参数。data是Ajax返回的原始数据,type是调用jQuery.ajax时提供的dataType参数。函数返回的值将由jQuery进一步处理。 function(data, type){ //返回处理后的数据 return data; } **15.global**: 要求为Boolean类型的参数,默认为true。表示是否触发全局ajax事件。设置为false将不会触发全局ajax事件,ajaxStart或ajaxStop可用于控制各种ajax事件。 **16.ifModified**: 要求为Boolean类型的参数,默认为false。仅在服务器数据改变时获取新数据。服务器数据改变判断的依据是Last-Modified头信息。默认值是false,即忽略头信息。 **17.jsonp**: 要求为String类型的参数,在一个jsonp请求中重写回调函数的名字。该值用来替代在"callback=?"这种GET或POST请求中URL参数里的"callback"部分,例如{jsonp:'onJsonPLoad'}会导致将"onJsonPLoad=?"传给服务器。 **18.username**: 要求为String类型的参数,用于响应HTTP访问认证请求的用户名。 **19.password**: 要求为String类型的参数,用于响应HTTP访问认证请求的密码。 **20.processData**: 要求为Boolean类型的参数,默认为true。默认情况下,发送的数据将被转换为对象(从技术角度来讲并非字符串)以配合默认内容类型"application/x-www-form-urlencoded"。如果要发送DOM树信息或者其他不希望转换的信息,请设置为false。 **21.scriptCharset**: 要求为String类型的参数,只有当请求时dataType为"jsonp"或者"script",并且type是GET时才会用于强制修改字符集(charset)。通常在本地和远程的内容编码不同时使用。 **22.$.each()函数**: $.each()函数不同于JQuery对象的each()方法,它是一个全局函数,不操作JQuery对象,而是以一个数组或者对象作为第1个参数,以一个回调函数作为第2个参数。回调函数拥有两个参数:第1个为对象的成员或数组的索引,第2个为对应变量或内容。 案例代码: \[![](https://common.cnblogs.com/images/copycode.gif)\](javascript:void(0); "复制代码") ``` <pre class="calibre13">``` <span class="token3">$</span><span class="token1">(</span><span class="token2">function</span><span class="token1">(</span><span class="token1">)</span><span class="token1">{</span> <span class="token3">$</span><span class="token1">(</span><span class="token4">'#send'</span><span class="token1">)</span><span class="token1">.</span><span class="token3">click</span><span class="token1">(</span><span class="token2">function</span><span class="token1">(</span><span class="token1">)</span><span class="token1">{</span> $<span class="token1">.</span><span class="token3">ajax</span><span class="token1">(</span><span class="token1">{</span> type<span class="token1">:</span> <span class="token4">"GET"</span><span class="token1">,</span> url<span class="token1">:</span> <span class="token4">"test.json"</span><span class="token1">,</span> data<span class="token1">:</span> <span class="token1">{</span>username<span class="token1">:</span><span class="token3">$</span><span class="token1">(</span><span class="token4">"#username"</span><span class="token1">)</span><span class="token1">.</span><span class="token3">val</span><span class="token1">(</span><span class="token1">)</span><span class="token1">,</span> content<span class="token1">:</span><span class="token3">$</span><span class="token1">(</span><span class="token4">"#content"</span><span class="token1">)</span><span class="token1">.</span><span class="token3">val</span><span class="token1">(</span><span class="token1">)</span><span class="token1">}</span><span class="token1">,</span> dataType<span class="token1">:</span> <span class="token4">"json"</span><span class="token1">,</span> success<span class="token1">:</span> <span class="token2">function</span><span class="token1">(</span>data<span class="token1">)</span><span class="token1">{</span> <span class="token3">$</span><span class="token1">(</span><span class="token4">'#resText'</span><span class="token1">)</span><span class="token1">.</span><span class="token3">empty</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//清空resText里面的所有内容</span> <span class="token2">var</span> html <span class="token">=</span> <span class="token4">''</span><span class="token1">;</span> $<span class="token1">.</span><span class="token3">each</span><span class="token1">(</span>data<span class="token1">,</span> <span class="token2">function</span><span class="token1">(</span>commentIndex<span class="token1">,</span> comment<span class="token1">)</span><span class="token1">{</span> html <span class="token">+=</span> <span class="token4">'<div class="comment"><h6>'</span> <span class="token">+</span> comment<span class="token1">[</span><span class="token4">'username'</span><span class="token1">]</span> <span class="token">+</span> <span class="token4">':</h6><p class="para"'</span> <span class="token">+</span> comment<span class="token1">[</span><span class="token4">'content'</span><span class="token1">]</span> <span class="token">+</span> <span class="token4">'</p></div>'</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">$</span><span class="token1">(</span><span class="token4">'#resText'</span><span class="token1">)</span><span class="token1">.</span><span class="token3">html</span><span class="token1">(</span>html<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> ``` ``` \[![](https://common.cnblogs.com/images/copycode.gif)\](javascript:void(0); "复制代码") $.ajax验证登录: \[![](https://common.cnblogs.com/images/copycode.gif)\](javascript:void(0); "复制代码") ``` <pre class="calibre13">``` <span class="token"><</span>script type<span class="token">=</span><span class="token4">"text/javascript"</span> language<span class="token">=</span><span class="token4">"javascript"</span><span class="token">></span> <span class="token2">function</span> <span class="token3">IbtnEnter_onclick</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token3">checklogin</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">function</span> <span class="token3">checklogin</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">$</span><span class="token1">(</span><span class="token4">"#TxtUserName"</span><span class="token1">)</span><span class="token1">.</span><span class="token3">val</span><span class="token1">(</span><span class="token1">)</span> <span class="token">==</span> <span class="token4">""</span><span class="token1">)</span> <span class="token1">{</span> <span class="token3">alert</span><span class="token1">(</span><span class="token4">"用户名不能为空!"</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">$</span><span class="token1">(</span><span class="token4">"#TxtUserName"</span><span class="token1">)</span><span class="token1">.</span><span class="token3">focus</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">$</span><span class="token1">(</span><span class="token4">"#TxtPassword"</span><span class="token1">)</span><span class="token1">.</span><span class="token3">val</span><span class="token1">(</span><span class="token1">)</span> <span class="token">==</span> <span class="token4">""</span><span class="token1">)</span> <span class="token1">{</span> <span class="token3">alert</span><span class="token1">(</span><span class="token4">"密码不能为空!"</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">$</span><span class="token1">(</span><span class="token4">"#TxtPassword"</span><span class="token1">)</span><span class="token1">.</span><span class="token3">focus</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token1">}</span> $<span class="token1">.</span><span class="token3">ajax</span><span class="token1">(</span><span class="token1">{</span> type<span class="token1">:</span> <span class="token4">"POST"</span><span class="token1">,</span> url<span class="token1">:</span> <span class="token4">"ajax/Handler.ashx?M="</span> <span class="token">+</span> Math<span class="token1">.</span><span class="token3">random</span><span class="token1">(</span><span class="token1">)</span><span class="token1">,</span> data<span class="token1">:</span> <span class="token4">"username="</span> <span class="token">+</span> <span class="token3">$</span><span class="token1">(</span><span class="token4">"#TxtUserName"</span><span class="token1">)</span><span class="token1">.</span><span class="token3">val</span><span class="token1">(</span><span class="token1">)</span><span class="token1">.</span><span class="token3">toString</span><span class="token1">(</span><span class="token1">)</span> <span class="token">+</span> <span class="token4">"&pwd="</span> <span class="token">+</span> <span class="token3">$</span><span class="token1">(</span><span class="token4">"#TxtPassword"</span><span class="token1">)</span><span class="token1">.</span><span class="token3">val</span><span class="token1">(</span><span class="token1">)</span><span class="token1">.</span><span class="token3">toString</span><span class="token1">(</span><span class="token1">)</span><span class="token1">,</span> success<span class="token1">:</span> <span class="token2">function</span> <span class="token1">(</span>data<span class="token1">)</span> <span class="token1">{</span> <span class="token2">if</span> <span class="token1">(</span>data <span class="token">==</span> <span class="token4">"1"</span><span class="token1">)</span> <span class="token1">{</span> location<span class="token1">.</span>href <span class="token">=</span> <span class="token4">"index.aspx"</span><span class="token1">;</span> <span class="token2">return</span> <span class="token5">true</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> <span class="token3">alert</span><span class="token1">(</span><span class="token4">"请确认您输入的用户名或密码输入是否正确!"</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">$</span><span class="token1">(</span><span class="token4">"#TxtUserName"</span><span class="token1">)</span><span class="token1">.</span><span class="token3">val</span><span class="token1">(</span><span class="token4">""</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">$</span><span class="token1">(</span><span class="token4">"#TxtPassword"</span><span class="token1">)</span><span class="token1">.</span><span class="token3">val</span><span class="token1">(</span><span class="token4">""</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">$</span><span class="token1">(</span><span class="token4">"#TxtUserName"</span><span class="token1">)</span><span class="token1">.</span><span class="token3">focus</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span> <span class="token1">}</span> <span class="token"><</span><span class="token">/</span>script<span class="token">></span> ``` ``` \[![](https://common.cnblogs.com/images/copycode.gif)\](javascript:void(0); "复制代码") ``` <pre class="calibre13">``` 一般处理程序 ``` ``` \[![](https://common.cnblogs.com/images/copycode.gif)\](javascript:void(0); "复制代码") ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">%</span>@ WebHandler Language<span class="token">=</span><span class="token4">"C#"</span> Class<span class="token">=</span><span class="token4">"Handler"</span> <span class="token">%</span><span class="token">></span> using System<span class="token1">;</span> using System<span class="token1">.</span>Web<span class="token1">;</span> using System<span class="token1">.</span>Data<span class="token1">.</span>SqlClient<span class="token1">;</span> using System<span class="token1">.</span>Web<span class="token1">.</span>SessionState<span class="token1">;</span><span class="token6">//继承接口IReadOnlySessionState需要引入的命名空间</span> <span class="token2">public</span> <span class="token2">class</span> <span class="token3">Handler</span> <span class="token1">:</span> IHttpHandler<span class="token1">,</span> IRequiresSessionState <span class="token1">{</span> SqlHelper helper <span class="token">=</span> <span class="token2">new</span> <span class="token3">SqlHelper</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">public</span> <span class="token2">void</span> <span class="token3">ProcessRequest</span><span class="token1">(</span>HttpContext context<span class="token1">)</span> <span class="token1">{</span> context<span class="token1">.</span>Response<span class="token1">.</span>ContentType <span class="token">=</span> <span class="token4">"text/plain"</span><span class="token1">;</span> string username <span class="token">=</span> context<span class="token1">.</span>Request<span class="token1">.</span>Params<span class="token1">[</span><span class="token4">"username"</span><span class="token1">]</span><span class="token1">.</span><span class="token3">ToString</span><span class="token1">(</span><span class="token1">)</span><span class="token1">.</span><span class="token3">Trim</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> string pwd <span class="token">=</span> context<span class="token1">.</span>Request<span class="token1">.</span>Params<span class="token1">[</span><span class="token4">"pwd"</span><span class="token1">]</span><span class="token1">.</span><span class="token3">ToString</span><span class="token1">(</span><span class="token1">)</span><span class="token1">.</span><span class="token3">Trim</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span>username <span class="token">!=</span> <span class="token4">""</span> <span class="token">&&</span> pwd <span class="token">!=</span> <span class="token4">""</span><span class="token1">)</span> <span class="token1">{</span> string sql <span class="token">=</span> @<span class="token4">"SELECT * FROM [USER] WHERE USERNAME='"</span><span class="token">+</span>username<span class="token">+</span><span class="token4">"' AND PASSWORD='"</span><span class="token">+</span>pwd<span class="token">+</span><span class="token4">"' "</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span><span class="token">!</span>helper<span class="token1">.</span><span class="token3">Exists</span><span class="token1">(</span>sql<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> context<span class="token1">.</span>Response<span class="token1">.</span><span class="token3">Write</span><span class="token1">(</span><span class="token4">"0"</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> SqlDataReader reader <span class="token">=</span> helper<span class="token1">.</span><span class="token3">ExecuteReader</span><span class="token1">(</span>sql<span class="token1">)</span><span class="token1">;</span> <span class="token2">while</span> <span class="token1">(</span>reader<span class="token1">.</span><span class="token3">Read</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> context<span class="token1">.</span>Response<span class="token1">.</span><span class="token3">Write</span><span class="token1">(</span><span class="token4">"1"</span><span class="token1">)</span><span class="token1">;</span> context<span class="token1">.</span>Session<span class="token1">[</span><span class="token4">"username"</span><span class="token1">]</span> <span class="token">=</span> username<span class="token1">.</span><span class="token3">ToString</span><span class="token1">(</span><span class="token1">)</span><span class="token1">.</span><span class="token3">Trim</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> context<span class="token1">.</span>Session<span class="token1">[</span><span class="token4">"pwd"</span><span class="token1">]</span> <span class="token">=</span> pwd<span class="token1">.</span><span class="token3">ToString</span><span class="token1">(</span><span class="token1">)</span><span class="token1">.</span><span class="token3">Trim</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token2">public</span> bool IsReusable <span class="token1">{</span> <span class="token2">get</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` \[![](https://common.cnblogs.com/images/copycode.gif)\](javascript:void(0); "复制代码")