学习Ajax框架之dojo:第三节——dojo中xmlHttp的用法(附源代码)

      在第二节中,介绍了简单的DOM操作方法,接下来该到Ajax的传统项目-xmlHttpRequest了。在使用xmlHttpRequest时,需要注意到编码的问题,要让dojo默认绑定为utf-8怎么办呢?很简单,只需要修改一下引入dojo.js时的标签:

<script type="text/javascript" src="./dojo-lib/dojo/dojo.js" djConfig="isDebug:true,bindEncoding:'UTF-8'"></script>

 

      下面,我们用实例来说明dojo中如何使用xmlHttpRequest。

实例一:

功能:简单的调用JSP页面的内容,没有参数的传递。

源代码如下:

Html页面,即主页面

方法1:

<html>
    <head>
        <meta name="generator" content="HTML Tidy, see http://www.w3.org/">
        <meta http-equiv="Content-Type" content= "text/html; charset=iso-8859-1">
        <title>Dojo, xmlHttpRequest!</title>
        <script type="text/javascript" src="dojoroot/dojo/dojo.js" djConfig="isDebug:true,bindEncoding:'UTF-8'">
        </script>
    </head>
    <body>
        <input id="hello" value="hello" type="button"/><input id="dropContent" value="This is my first page!" type="text"/>
        <div id="divHello">
        </div>
        <script type="text/javascript"> 
            function responsevalue(responseText){
                alert(responseText);
                dojo.byId("dropContent").value = responseText;
            }           
            function responseError(response){
                alert("Error");
            }           
            function helloworld(){
                dojo.xhrGet({
                    url: "test.jsp",
                    method: "Get",
                    handleAs: "text",
                    load: responsevalue,
                    error: responseError,
                });
            }
           
            var first = dojo.byId("hello");
            dojo.connect(first, "onclick", helloworld);
        </script>
    </body>
</html>

方法2:

<html>
    <head>
        <meta name="generator" content="HTML Tidy, see http://www.w3.org/">
        <meta http-equiv="Content-Type" content= "text/html; charset=iso-8859-1">
        <title>Dojo, xmlHttpRequest!</title>
        <script type="text/javascript" src="dojo-0.3.1-ajax/dojo.js" djConfig="isDebug:true,bindEncoding:'UTF-8'">
        </script>
    </head>
    <body>
        <input id="hello" value="hello" type="button"/><input id="dropContent" value="This is my first page!" type="text"/>
        <div id="divHello">
        </div>
        <script type="text/javascript">
            function helloworld1(){
                dojo.io.bind({
                    url: "test.jsp",
                    load: function(type, data, evt){
                        dojo.byId("divHello").innerHTML = data;
                    },
                    error: function(type, error){          
                        alert(error);
                    }
                });
            }
            var first = dojo.byId("hello");
            dojo.event.connect(first, "onclick", helloworld1);
        </script>
    </body>
</html>

test.jsp页面:

<%@ page contentType="text/html;charset=GB2312"%>

<% 
     String result =“Hello, Dojo, xmlHttpRequest !”;
     out.print(result);
%>

 

输出结果:运行Html页面,点击“hello”按钮时,输出结果为:Hello, Dojo, xmlHttpRequest !

 

 

实例二:

功能:调用JSP页面的内容,如何传递参数。在实例一给出代码中添加参数content即可。

源代码如下:

Html页面: 

var params = {
     username: 'feifei',
     id: '20022458',
}

function helloworld(){
          dojo.xhrGet({
          url: "testparams.jsp",
          content: params,
          method: "Get",
          handleAs: "text",
          load: responsevalue,
          error: responseError,
          });
}


testparams.jsp页面:

<%@ page contentType="text/html;charset=GB2312"%>
<% 
      String username = request.getParameter("username");
      String password=request.getParameter("id");
      String result =username;
      result+= password;
      out.print(result);
%>

 

输出结果:运行Html页面,点击“hello”按钮时,输出结果为:feifei20022458

转载于:https://www.cnblogs.com/penny/archive/2008/08/30/1279797.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值