官方教程:Ajax with Dojo
Ajax功能:
1.从服务器加载静态数据
2.从web服务中访问xml或json类型的数据
3.将表单(form)数据发送到服务器
4.刷新页面内容
。。。。
Ajax在RIA中是核心技术,下面来看看Dojo是如何运用它的。

 // Require the xhr module

require(["dojo/_base/xhr",
"dojo/dom"],
    function(xhr,dom) {

// Local var representing the node to be
updated

var availabilityNode =
dom.byId(
"availabilityNode");

// Using xhr, as very little information is being sent
xhr.get({
    // The URL of the request
    url:
"check-username.php",
    // Allow only 2 seconds for username
check
    timeout: 2000,
    // Send the username to check base on an INPUT
node's value
    content: {
        username:
dom.byId(
"usernameInput").value.toLowerCase()
    },
    // The success callback with result from
server
    load: function(result)
{
        if(result ==
"available") {
            availabilityNode.innerHTML =
"Username available!";
        }
        else {
            availabilityNode.innerHTML =
"Username taken!  Please try another.";
        }
    },
   
// The error
handler

    error: function(errorMessage){
       
alert(errorMessage);
    }
,

    // The complete handler
    handle: function()
{
        hasBeenSent =
true;
    }
});
});

首先要引入包dojo/_base/xhr。
然后通过调用xhr.get或xhr.post这两个常用的方法来发送请求。
这两个方法的参数是一个对象,包含以下属性:
url:请求的服务器地址
handleAs:返回数据的格式,默认为"text",可选"json","javascript"(加载并执行),"xml"
timeout:请求超时,以毫秒记算,当超过这个时间视为请求失败并触发error处理函数
content:请求时发送的内容,是一个“键-值”对形式的对象。如果调用的是get方法,会被转换为一个字条串,如果是post方法,则转换为post内容体。
form:指定一个表单节点对象,将会发送表单内所有的数据,此时如果没有指定url,将会采用form属性action的值,如果同时设置content将会覆盖form中的内容,不建议同时使用

当服务器接受到请求并做出回应后,可以通过以下三个方法来处理:
load(response,
ioArgs):
请求成功时的回调函数,第一个参数就是根据handleAs指定的格式返回的数据内容。
error(errorMessage)
:请求失败时的回调函数,数据为失败的信息
handle(response, ioArgs)
:不论成功或失败都将调用的回调函数
根据请求是否成功,首先会调用load/error函数,然后调用handle

JSON数据结构
JSON是以“键-值”对为单位的数据结构,如{"name":"mqsy_yj",
"age":30,"ismale",true},键为"name",值为"mqsy_yj"。
“键-值”对之间用逗号分隔,大括号为一个JSON对象,多个JSON对象用逗号分隔并包含在一个中括号内为一个JSON对象数组。
在dojo中对JSON的处理方法为:
require(["dojo/_base/array"],function(array){
   
var jsonData = {"newsItems":[{"name":"mqsy_yj",
"age":30,"ismale",true},{"name":"mqsy_yj1", "age":31,"ismale",false}]};
   
var content =
"";

                // For every news item we
received...
                arrayUtil.forEach(jsonData.newsItems,
function(newsItem) {
                    // Build data from the JSON
                    content +=
"<h2>" + newsItem.name+
"</h2>";
                    content +=
"<p>" + newsItem.age+
"</p>";
                   
content +=
"<p>" + newsItem.ismale+
"</p>";
                });

});

dojo 十 ajax dojo/_base/xhr的更多相关文章

  1. dojo(四):ajax请求

    储备知识 1.在介绍新版本的ajax请求之前,需要先了解一些dojo/Deferreds. 初次听到“Deferred”这个概念,可能会觉得这是一个神秘的东西.实际上它在执行异步操作的时候非常强大,例 ...

  2. dojo 九 effects dojo/_base/fx 和 dojo/fx

    官方教程:Dojo Effects这里讲学习一下dojo如何实现淡入.淡出.滑动等效果.实现这些特殊的效果有两个包 dojo/_base/fx 和 dojo/fx.dojo/_base/fx 中提供了 ...

  3. dojo 官方翻译 dojo/_base/array 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/_base/array.html#dojo-base-array array模块dojo进行 ...

  4. Dojo API中文 Dojo内容模块概览,初学者

    官网:http://dojotoolkit.org/reference-guide/1.10/dojo/index.html#dojo-dojo的翻译 dojo 内容: dojo dojo/dojo ...

  5. dojo 官方翻译 dojo/aspect

    官网地址:http://dojotoolkit.org/reference-guide/1.10/dojo/aspect.html after() 定义:after(target, methodNam ...

  6. dojo.publish 和 dojo.subscribe

    原文链接:http://www.cnblogs.com/didi/archive/2010/06/13/1757894.html //dojo.publish 和 dojo.subscribe  :d ...

  7. dojo.byId、dojo.query、dojo.attr

    概述: dojo.byId(/*string*/id或/*DomNode*/node) 1.传入DOMNode返回传入的domNode; 2.传入id返回id为当前值的domNode dojo.que ...

  8. dojo.js --dojo Quick Start/dojo入门手册1

    我看了http://www.cnblogs.com/mylem/archive/2009/11/11/1600984.html这篇博客以后 ,就开始设计自己的代码,其实很多解释都是在我的代码里,所以就 ...

  9. Dojo入门:dojo中的事件处理

      JS为DOM添加事件 在原生的环境下,为DOM添加事件处理函数有多种方法: <input type="button" name="btn" value ...

随机推荐

  1. bzoj 3232 01分数规划+最大权封闭子图判定

    我们的目标是使v/c最小化,所以构造函数g(x)=v-x*c,那么 二分一个X,判断当时的v-x*c的值是多少,然后根据g(x)函数的 单调递减性来二分,判断,直到g(x)=0的时候当前的X就是答案. ...

  2. hdu 1301 Jungle Roads 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...

  3. The 15th Zhejiang University Programming Contest

    a  ZOJ 3860 求和大家不一样的那个数,签到,map水之 #include<cstdio> #include<map> using namespace std; map ...

  4. GPU crash unmap page access

    这类gpu crash是 texture 没有gpu address 调试方法 去看texture,  texture state 里面allocateMemoryBlock...这里面有gpuadd ...

  5. The Brain as a Universal Learning Machine

    The Brain as a Universal Learning Machine This article presents an emerging architectural hypothesis ...

  6. [工作积累] bitfield

    ISO/IEC 14882:2003: 9.6 Bit-fields [class.bit] A member-declarator of the form identifieropt : const ...

  7. nginx 杂记

    接触nginx一段时间,有些自己的心得,偶尔在网上会看到一些细小的知识点,总结于此 nginx是以多进程的方式来工作的.nginx在启动后,会有一个master进程和多个worker进程. maste ...

  8. input输入框的border-radius属性在IE8下的完美兼容

    在工作中我们发现搜索框大部分都是有圆角的,为此作为经验不足的前端人员很容易就想到,给input标签添加border-radius属性不就解决了嘛.不错方法确实是这样,但是不要忘了border-radi ...

  9. Python编程指南 chapter 1

    1.python使用方括号[]来存取一个序列中的某个数据项,像字符串.列表等包含若干数据项的序列都采用这种方法. 2.强制类型转换,int('24234'),str(235) 3.python中没有变 ...

  10. MariaDB Galera Cluster集群

    一.MariaDB Galera Cluster概要: 1.简述: MariaDB Galera Cluster 是一套在mysql innodb存储引擎上面实现multi-master及数据实时同步 ...