一个js写的ajax请求例子
上一篇写了辞职,想到辞职心情就是爽啊,因为打算找工作嘛,现在就想写着玩,之前学习的时候都只是json格式的ajax,现在写个xml格式的请求例子(鄙人小菜大虾见了勿笑哦,第一次写):
1.首先创建好目录:
如图:
2.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> <script src="/Demo/Js/JScript.js" type="text/javascript"></script> </head> <body> <form name="logonForm" action="#"> <input type="text" name="entrytitle"/> <input type="button" name="test" onclick="WebDemo.Common.XmlHttp.test()" value="点击测试" /> </form> <div>这个只是Demo</div> </body> </html>
3.Js
var Namespace = new Object(); // 全局对象仅仅存在register函数,参数为名称空间全路径,如"Grandsoft.GEA" Namespace.register = function (fullNS) { // 将命名空间切成N部分, 比如Grandsoft、GEA等 var nsArray = fullNS.split(‘.‘); var sEval = ""; var sNS = ""; for (var i = 0; i < nsArray.length; i++) { if (i != 0) sNS += "."; sNS += nsArray[i]; sEval += "if (typeof(" + sNS + ") == ‘undefined‘) " + sNS + " = new Object();" } if (sEval != "") eval(sEval); } Namespace.register("WebDemo.Common"); WebDemo.Common.XmlHttp = { ExecuteUrl: function (url) { xmlHttp = this.Request("GET", url); return xmlHttp.responseText; }, ExecuteXmlUrl: function (sendXml, url) { xmlHttp = this.Request("POST", url, sendXml); return xmlHttp.responseText; }, Request: function (method, url, data, callback) { var bAsync = false; if (callback != undefined && callback != null) bAsync = true; var oXmlHttp = this.Create(); oXmlHttp.open(method, url, bAsync); if (bAsync) oXmlHttp.onreadystatechange = function () { if (oXmlHttp.readyState == 4 && oXmlHttp.status == 200) callback(oXmlHttp.responseText); }; try { oXmlHttp.send(data); } catch (e) { alert("请求异常:" + e.Message + "\nUrl:" + url + "\n发送数据:" + data); } return oXmlHttp; }, Create: function () { var xmlHttp = null; try { if (window.ActiveXObject) xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); else if (window.XMLHttpRequest) xmlHttp = new XMLHttpRequest(); } catch (e) { } return xmlHttp; }, test: function () { var add = "Add"; var name = "添加"; var senderXml = "<Function id=‘" + add + "‘ Name=‘" + name + "‘></Function>"; // var doc=new ActiveXObject("Microsoft.XMLDOM"); // var Root=doc.createElement("Function"); // doc.appendChild(Root); // Root.setAttribute("id", "Add"); // Root.setAttribute("Name", "添加"); var senderUrl = "../Handel/Handler1.ashx"; var ret = WebDemo.Common.XmlHttp.ExecuteXmlUrl(senderXml, senderUrl); if (ret) { alert(ret); } } }
4.Handel
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xml; using System.Data.OleDb; using System.Data; namespace WebApplicationDemo.Handel { /// <summary> /// Handler1 的摘要说明 /// </summary> public class Handler1 : IHttpHandler { XmlDocument xmlDoc = new XmlDocument(); public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/xml;charset=utf-8"; if (context.Request.InputStream.Length > 0) { xmlDoc.Load(context.Request.InputStream); XmlElement xmlroot = xmlDoc.DocumentElement; string OptionType = xmlroot.Attributes["id"].Value; string strResult = string.Empty; switch (OptionType) { case "Add": strResult = AddData(context); break; case "Edit": strResult = EditData(context); break; case "Del": strResult = DelData(context); break; default: break; } context.Response.Write(strResult); } } private string EditData(HttpContext context) { return "EditData" ; } private string DelData(HttpContext context) { return "DelData"; } private string AddData(HttpContext context) { return "AddData"; } public bool IsReusable { get { return false; } } }
5.运行
6.接下来么好好找个工作。一定要淡定,码农伤不起啊。。希望大家多多指点
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。