zoukankan      html  css  js  c++  java
  • dojo Quick Start/dojo入门手册xmlhttp dojo.xhrGet

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

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

    多了一个djConfig属性,很简单,第一个isDebug是说是否打开FireBug的Console,第二个是xmlhttp使用的编码。第二个才是重点,设置了就一劳永逸了。 这次我们要点击了hello按钮后发出一个xmlhttp请求:

    function sayHello() {
        dojo.xhrGet({
            url: "http://localhost/hello/sayHello.jsp",
            handleAs: "text",
            load: function(responseText)
            {
              alert(responseText);
              dojo.byId("divHello").innerHTML = responseText;
            },
            error: function(response)
            {
              alert("Error");
            }
        });
    }
    dojo.connect(btn,"onclick",sayHello);

    看看,够不够一目了然? url 就是url…… ;handleAs 把获取的内容作为text/html ;load 成功时的回调函数;error 失败时的回调函数

    那如果要传入参数怎么办?

    var params = {
        username:'Mark',
        id:'105'
    }
    dojo.xhrGet({
        url: "http://localhost/hello/sayHello.jsp",
        content:params,
        //...
    });

    注意那个content参数,你要传入的参数是个关联数组/object,dojo会自动把参数解析出来,要使用post方法? dojo.xhrGet ---> dojo.xhrPost ,其他的还有,dojo.xhrPut、dojo.xhrDelete。

  • 相关阅读:
    [leedcode 77] Combinations
    [leedcode 76] Minimum Window Substring
    [leedcode 75] Sort Colors
    [leedcode 74] Search a 2D Matrix
    [leedcode 73] Set Matrix Zeroes
    [leedcode 71] Simplify Path
    [leedcode 70] Climbing Stairs
    [leedcode 69] Sqrt(x)
    [leedcode 67] Add Binary
    HDU1027-Ignatius and the Princess II(全排列问题next_permutation函数的使用)
  • 原文地址:https://www.cnblogs.com/soundcode/p/2117550.html
Copyright © 2011-2022 走看看