Dojo-ajax framework

xhrGet is the most important function in the XHR framework and is used the most frequently. Use it to request static text resources on the server, such as txt, xml, etc., and to obtain dynamic pages such as php, jsp, asp, etc., as long as the character data stream is returned from the server.

In addition to xhrGet, Dojo’s XHR framework also includes xhrPost, rawXhrPost, xhrPut, rawXhrPut, xhrDelete. These functions are similar to xhrGet, and you can refer to xhrGet for the usage and parameters. The difference lies in their HTTP request type. xhrPost sends a Post request, xhrPut sends a Put request, and xhrDelete sends a Delete request.

Let’s look at a few examples:< /p>

< span style="line-height:1.8">1,Use xhrGet request text resource

Client——

  1. <%@ PageLanguage =“C#”AutoEventWireup< span style="margin:0px; padding:0px; border:none; background-color:inherit">=“true”CodeBehind=“HelloDojoAjax.aspx.cs”< span style="margin:0px; padding:0px; border:none; background-color:inherit">
  2. Inherits=“DojoTest.HelloDojoAjax”%>
  3. >< /span>
  4. <htmlxmlns=“http://www.w3.org/1999/xhtml”>
  5. <headrunat =“server”> li>
  6. <title>title> span>
  7. <scriptsrc=“http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js” type=“text/javascript”> script>
  8. < scripttype=“text/javascript”> li>
  9. function helloWorld(){
  10. Dojo.xhrGet({
  11.      url:”HelloDojo.txt”,//Requested server resource url
  12.      HandleAs:”text”,//Returned data Type
  13.      load:function(response,ioArgs){alert(response);},//Callback function after success
  14.      error:function(error,ioArgs){alert(error.message);}//Callback function when an error occurs
  15.   });
  16. }
  17. //Initialization function after binding page loading
  18. dojo.ready(helloWorld);
  19. script>
  20. head>
  21. <body> li>
  22. body>
  23. html span>>

Server-side resources——

  1. hello world!!Dojo!

2, use xhrGet to obtainJson Data

Client——

< div class="dp-highlighter bg_html" style="font-family:Consolas,'Courier New',Courier,mono,serif; overflow:auto; word-break:break-word; color:rgb(51,51,51 ); margin:18px 0px!important">

  1. <%@ PageLanguage=“C#”< span style="margin:0px; padding:0px; border:none; background-color:inherit">AutoEventWireup=“true”CodeBehind=“DojoAjaxJson.aspx.cs”Inherits= “DojoTest.DojoAjaxJson”%>
  2. >
  3. <htmlxmlns =“http://www.w3.org/1999/xhtml”>
  4. < headrunat=“server”>
  5. < li style="padding:0px; border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal outside; background-color:rgb(248,248,248 ); line-height:1.6; margin:0px!important"> <title> title>

  6. <%– Introduce Dojo–%>
  7.     <script src=” http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js” type=“text/javascript”>script>  
  8.       
  9.     <script type=“text/javascript”>  
  10.         function GetJsonData() {  
  11.             dojo.xhrGet({  
  12.                 url: “GetCity.aspx”, //请求的服务器资源url  
  13.                 handleAs: “json”, //返回的数据类型  
  14.                 handle: PrintResult//回调函数  
  15.             });  
  16.             return false;  
  17.         }  
  18.   
  19.   
  20.         //对返回的json数据进行处理,放入表格  
  21.         function PrintResult(data){  
  22.             
  23.             var table = “<table border=\”1\”>“;  
  24.             table += “<tr><th>ProvinceIdth><th>CityNameth>tr>“;  
  25.             dojo.forEach(data, function(city) {  
  26.                 table += “<tr><td>“;  
  27.                 table += city.ProvinceId;  
  28.                 table += “td><td>“;  
  29.                 table += city.CityName;  
  30.                 table += “td>tr>“;  
  31.             });  
  32.             table += “table>“;  
  33.             dojo.place(table, dojo.body());  
  34.        }  
  35.   
  36.        function init() {  
  37.            //helloworld 函数到按钮的点击事件  
  38.            dojo.connect(dojo.byId(“mybutton”), “onclick”, “GetJsonData”);  
  39.        }  
  40.   
  41.        //绑定页面加载完成后的初始化函数  
  42.        dojo.ready(init);  
  43.     script>  
  44. head>  
  45. <body>  
  46.       <input type=“button” id=“mybutton” value=“获取json数据” />  
  47. body>  
  48. html>  

 

服务端——

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace JqueryAjaxTest.Data  
  9. {  
  10.     public partial class GetCity : System.Web.UI.Page  
  11.     {  
  12.         
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.             string r esult = @”  
  16. [{“”ProvinceId””:””BJ””,””CityName””:””北京””},  
  17.  {“”ProvinceId””:””TJ””,””CityName””:””天津””}]”;  
  18.   
  19.            //清空缓冲区  
  20.            Response.Clear();  
  21.            //将字符串写入响应输出流  
  22.            Response.Write(result);  
  23.            //将当前所有缓冲的输出发送的客户端,并停止该页执行  
  24.            Response.End();  
  25.         }  
  26.   
  27.     }  
  28. }  

 

3、使用xhrGet提交表单

客户端——

  1. <%@ Page Language=“C#” AutoEventWireup= “true” CodeBehind=“DojoAjaxText.aspx.cs” Inherits=“DojoTest.DojoAjaxText” %>  
  2.   
  3. >  
  4. <html xmlns=“http://www.w3.org/1999/xhtml”>  
  5. <head runat=“server”>  
  6.     <title>title>  
  7.     <%– 引入 Dojo–%>  
  8.     <script src=“http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js” type=“text/javascript”>script>  
  9.     <script type=“text/javascript”>  
  10.   
  11.         function SubmitForm() {  
  12.             dojo.xhrGet({  
  13.                 url:”GetService.aspx”,  
  14.                 form: “myform”, //需要异步提交的表单的 id  
  15.                 handleAs: “text”, //默认值,不对返回的数据做任何处理  
  16.                 handle: PrintResult, //正常和错误返回的情况都能处理,可以说是 load 和 error 的混合体,但优先级比 load 低,只有在没有设置 load 时才起作用。  
  17.                 content:{Password:”123456″},//这里可以修改表单中的内容,如果起始表单都为空,则修改无效  
  18.                 sync:false //默认为异步,所设不设false意义不大  
  19.   
  20.             });  
  21.             return false; //为了阻止系统默认的表单提交事件,让表单提交异步进行,如果不返回 false,会引起页面跳转。  
  22.         }  
  23.   
  24.         function PrintResult(response){  
  25.             dojo.create(  
  26.                 “div”,  
  27.                 {  
  28.                     “innerHTML”: response  
  29.                 },  
  30.                 dojo.body()  
  31.               );  
  32.         }  
  33.     script>  
  34. head>  
  35. <body>  
  36.     <form id=“myform” < span class="attribute" style="margin:0px; padding:0px; border:none; background-color:inherit">onsubmit=“return SubmitForm();”>  
  37.         用户名:<input type=“text” name=“UserID” />  
  38.         密码:<input type=“password” name=“Password” />  
  39.         <input type=“submit” name=“sub” value=“提交” />  
  40.     form>  
  41. body>  
  42. html>  

服务端——

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace DojoTest  
  9. {  
  10.     public partial class GetService : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.             string id = “”;  
  15.             string pwd = “”;  
  16.             //获取参数  
  17.             if (!String.IsNullOrEmpty(HttpContext.Current.Request[“UserID”]) && !String.IsNullOrEmpty(HttpContext.Current.Request[“Password”]))  
  18.             {  
  19.                 id = HttpContext.Current.Request[“UserID”];  
  20.                 pwd=HttpContext.Current.Request[“Password”];  
  21.             }  
  22.   
  23.             //清空缓冲区  
  24.             Response.Clear();  
  25.             //将字符串写入响应输出流  
  26.             Response.Write(“用户输入id为:”+id+”,输入密码为:”+pwd);  
  27.             //将当前所有缓冲的输出发送的客户端,并停止该页执行  
  28.             Response.End();  
  29.   
  30.         }  
  31.     }  
  32. }  

 

注意:

1

回调函数PrintResult包含两个参数:response 和 ioArgs。

response:表示从服务器端返回的数据,Dojo 已经根据 handleAs 设置的数据类型进行了预处理。

ioArgs: 这是一个对象,包含调用 xhrGet 时使用的一些参数。之所以把这些信息放在一个对象中并传递给回调函数是为了给回调函数一个执行“上下文”,让回调函数知道自己属于哪个 HTTP 请求,请求有哪些参数,返回的数据是什么类型等。这些信息在调试程序时特别有用。

ioArgs.url:请求的 URL,与调用 xhrGet 时设置的值一样。

ioArgs.query:请求中包含的参数, URL 中“ ? ”后面的内容。

ioArgs.handAs:如何对返回的数据进行预处理,与调用 xhrGet 时设置的值一样。

ioArgs.xhr: xhrGet 函数使用的 XHR 对象。

 

2handleAs预处理方式

text默认值,不对返回的数据做任何处理

xml返回 XHR 对象的 responseXML

javascript使用 dojo.eval 处理返回的数据,返回处理结果

json使用 dojo.fromJSon 来处理返回的数据,返回生成的 Json 对象

json-comment-optional如果有数据包含在注释符中,则只使用 dojo.fromJSon 处理这部分数据,如果没有数据包含在注释符中,则使用 dojo.fromJSon 处理全部数据。

json-comment-filtered数据应该包含在 /* … */ 中,返回使用 dojo.fromJSon 生成的 Json 对象,如果数据不是包含在注释符中则不处理。

 

3、代码中的注释,也说明了一些值得注意的地方。

  1. <%@ Page Language=“C#” AutoEventWireup=“true” CodeBehind=“HelloDojoAjax.aspx.cs”  
  2.     Inherits=“DojoTest.HelloDojoAjax” %>  
  3.   
  4. >  
  5. <html xmlns=“http://www.w3.org/1999/xhtml”>  
  6. <head runat=“server”>  
  7.     <title>title>   
  8.     <script src=“http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js” type=“text/javascript”>script>  
  9.     <script type=“text/javascript”>  
  10.   
  11.       function helloWorld(){  
  12.           dojo.xhrGet({  
  13.             url:”HelloDojo.txt”,//请求的服务器资源url  
  14.             handleAs:”text”,//返回的数据类型  
  15.             load:function(response,ioArgs){alert(response);},//成功后回调函数  
  16.             error:function(error,ioArgs){alert(error.message);}//出错时回调函数  
  17.           });  
  18.        }  
  19.   
  20.        //绑定页面加载完成后的初始化函数  
  21.        dojo.ready(helloWorld);  
  22.     script>  
  23. head>  
  24. <body>  
  25.      
  26. body>  
  27. html>  
  1. hello world!!Dojo!  
  1. <%@ Page Language=“C#”< /span> AutoEventWireup=“true” CodeBehind=“DojoAjaxJson.aspx.cs” I nherits=“DojoTest.DojoAjaxJson” %>  
  2.   
  3. >  
  4. <html xmlns=“http://www.w3.org/1999/xhtml”>  
  5. <head runat=“server”>  
  6.     <title>title>  
  7.     <%– 引入 Dojo–%>  
  8.     <script src=“http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js” type=“text/javascript”>script>  
  9.       
  10.     <script type=“text/javascript”>  
  11.         function GetJsonData() {  
  12.             dojo.xhrGet({  
  13.                 url: “GetCity.aspx”, //请求的服务器资源url  
  14.                 handleAs: “json”, //返回的数据类型  
  15.                 handle: PrintResult//回调函数  
  16.             });  
  17.             return false;  
  18.         }  
  19.   
  20.   
  21.         //对返回的json数据进行处理,放入表格  
  22.         function PrintResult(data){  
  23.             
  24.             var table = “<table border=\”1\”>“;  
  25.             table += “<tr><th>ProvinceIdth><th>CityNameth>tr>“;  
  26.             dojo.forEach(data, function(city) {  
  27.                 table += “<tr><td>“;  
  28.                 table += city.ProvinceId;  
  29.                 table += “td><td>“;  
  30.                 table += city.CityName;  
  31.                 table += “td>tr>“;  
  32.             });  
  33.             table += “table>“;  
  34.             dojo.place(table, dojo.body());  
  35.        }  
  36.   
  37.        function init() {  
  38.            //helloworld 函数到按钮的点击事件  
  39.            dojo.connect(dojo.byId(“mybutton”), “onclick”, “GetJsonData”);  
  40.        }  
  41.   
  42.        //绑定页面加载完成后的初始化函数  
  43.        dojo.ready(init);  
  44.     script>  
  45. head>  
  46. <body>  
  47.       <input type=“button” id=“mybutton” value=“获取json数据” />  
  48. body>  
  49. < span class="tag-name" style="margin:0px; padding:0px; border:none; background-color:inherit">html>  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. usi ng System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace JqueryAjaxTest.Data  
  9. {  
  10.     public partial class GetCity : System.Web.UI.Page  
  11.     {  
  12.         
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.             string result = @”  
  16. [{“”ProvinceId””:””BJ””,””CityName””:””北京””},  
  17.  {“”ProvinceId””:””TJ””,””CityName””:””天津””}]”;  
  18.   
  19.            //清空缓冲区  
  20.            Response.Clear();  
  21.            //将字符串写入响应输出流  
  22.            Response.Write(result);  
  23.            //将当前所有缓冲的输出发送的客户端,并停止该页执行  
  24.            Response.End();  
  25.         }  
  26.   
  27.     }  
  28. }  
  1. <%@ Page Language=“C#” AutoEventWireup=“true” CodeBehind=“DojoAjaxText.aspx.cs” Inherits=“DojoTest.DojoAjaxText” %>  
  2.   
  3. >  
  4. <html xmlns=“http://www.w3.org/1999/xhtml”>  
  5. <head runat=“server”>  
  6.     <title>title>  
  7.     <%– 引入 Dojo–%>  
  8.     <script src=“http://ajax.googl eapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js” type=“text/javascript”>script>  
  9.     <script type=“text/javascript”>  
  10.   
  11.         function SubmitForm() {  
  12.             dojo.xhrGet({  
  13.                 url:”GetService.aspx”,  
  14.                 form: “myform”, //需要异步提交的表单的 id  
  15.                 handleAs: “text”, //默认值,不对返回的数据做任何处理  
  16.                 handle: PrintResult, //正常和错误返回的情况都能处理,可以说是 load 和 error 的混合体,但优先级比 load 低,只有在没有设置 load 时才起作用。  
  17.                 content:{Password:”123456″},//这里可以修改表单中的内容,如果起始表单都为空,则修改无效  
  18.                 sync:false //默认为异步,所设不设false意义不大  
  19.   
  20.             });  
  21.             return false; //为了阻止系统默认的表单提交事件,让表单提交异步进行,如果不返回 false,会引起页面跳转。  
  22.         }  
  23.   
  24.         function PrintResult(response){  
  25.             dojo.create(  
  26.                 “div”,  
  27.                 {  
  28.                     “innerHTML”: response  
  29.                 },  
  30.                 dojo.body()  
  31.               );  
  32.         }  
  33.     script>  
  34. head>  
  35. <body>  
  36.     <form id=“myform” onsubmit=“return SubmitForm();”>  
  37.         用户名:<input type=“text” name=“UserID” />  
  38.         密码:<input type=“password” name=“Password” />  
  39.         <input type=“submit” name=“sub” value=“提交” />  
  40.     form>  
  41. body>  
  42. html>  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace DojoTest  
  9. {  
  10.     public partial class GetService : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.             string id = “”;  
  15.             string pwd = “”;  
  16.             //获取参数  
  17.             if (!String.IsNullOrEmpty(HttpContext.Current.Request[“UserID”]) && !String.IsNullOrEmpty(HttpContext.Current.Request[“Password”]))  
  18.             {  
  19.                 id = HttpContext.Current.Request[“UserID”];  
  20.                 pwd=HttpContext.Current.Request[“Password”];  
  21.             }  
  22.   
  23.             //清空缓冲区  
  24.             Response.Clear();  
  25.             //将字符串写入响应输出流  
  26.             Response.Write(“用户输入id为:”+id+”,输入密码为:”+pwd);  
  27.             //将当前所有缓冲的输出发送的客户端,并停止该页执行  
  28.             Response.End();  
  29.   
  30.         }  
  31.     }  
  32. }  

Leave a Comment

Your email address will not be published.