jQuery学习笔记之Ajax用法实例详解(3)

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.SessionState; using System.Xml; namespace MyJqTest { public class AjaxHandler : IHttpHandler, IRequiresSessionState { /// <summary> /// 可复用 /// </summary> public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { AjaxOperations(context); } private void AjaxOperations(HttpContext context) { string action = context.Request["action"]; if (!string.IsNullOrEmpty(action)) { switch (action) { default: break; case "jquery": ProcessJQueryRequest(context); break; case "jquerySaveData": ProcessJQuerySaveData(context); break; case "syncRequest": ProcessJQuerySyncRequest(context); break; case "jqueryXmlRequest": ProcessJQueryXMLRequest(context); break; case "jquerGetXmlRequest": ProcessJQueryGetXMLRequest(context); break; } } } private void ProcessJQueryRequest(HttpContext context) { context.Response.ClearContent(); context.Response.ContentType = "text/plain"; //设置输出流类型 context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //没有缓存 string result = context.Request["userName"].Trim(); context.Response.Write("You have entered a name:" + result); } private void ProcessJQuerySaveData(HttpContext context) { context.Response.ClearContent(); context.Response.ContentType = "text/plain"; //设置输出流类型 context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //没有缓存 string name = context.Request["userName"].Trim(); string location = context.Request["location"].Trim(); context.Response.Write("Your data have been saved:your name is " + name + ",living in " + location); } private void ProcessJQuerySyncRequest(HttpContext context) { context.Response.ClearContent(); context.Response.ContentType = "text/plain"; //设置输出流类型 context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //没有缓存 context.Response.Write("Your sync ajax request has been processed."); } /// <summary> /// 简单的xml请求处理(服务端从客户端获取xml) /// </summary> /// <param></param> private void ProcessJQueryXMLRequest(HttpContext context) { context.Response.ClearContent(); context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //没有缓存 XmlDocument doc = new XmlDocument(); try { doc.Load(context.Request.InputStream); //获取xml (这里需要注意接受xml数据的方法) context.Response.ContentType = "text/plain"; //设置输出流类型 string name = doc.SelectSingleNode("profile/userName").InnerText; string location = doc.SelectSingleNode("profile/location").InnerText; context.Response.Write("Your XML data have received,and your name is " + name + ",living in " + location); } catch (Exception ex) { context.Response.Write("Get xml data failed."); throw ex; } } /// <summary> /// 返回简单的xml(服务端返回客户端xml) /// </summary> /// <param></param> private void ProcessJQueryGetXMLRequest(HttpContext context) { context.Response.ClearContent(); context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //没有缓存 XmlDocument doc = new XmlDocument(); try { doc.Load(context.Server.MapPath("/jeffWong.xml")); context.Response.ContentType = "text/xml;charset=UTF-8"; //设置输出流类型 context.Response.Write(doc.OuterXml); } catch (Exception ex) { context.Response.Write("Load xml data failed."); throw ex; } } } }

b、aspx,html和xml文件(直接放在根目录下)

aspx文件是ajax请求页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JQuery.aspx.cs" Inherits="MyJqTest.JQuery" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="https://www.jb51.net/js/jquery-1.3.1.js" type="text/javascript"></script> </head> <body> <form runat="server"> <div> &nbsp;&nbsp;<span></span> <br /> Please input a name:<input type="text" /><input type="button" value="jQuery ajax 请求" /> &nbsp;&nbsp;<span></span> </div> <script src="https://www.jb51.net/js/jQTest.js" type="text/javascript"></script> </form> </body> </html>

html很简单:

test.htm:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title></title> </head> <body> <div>it is a simple ajax test</div> </body> </html>

xml文件:

jeffWong.xml:

<?xml version="1.0" encoding="utf-8" ?> <profile> <userName>jeff wong</userName> <location>beijing</location> </profile>

c、js文件(放在根目录js文件夹下)

jqLoadJs.js  测试ajax加载js文件用

复制代码 代码如下:

alert("this is a ajax request script test");

2、load(url,[data],[callback])

载入远程 HTML 文件代码并插入至 DOM 中。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgzgwd.html