注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

星期五

明天不上班

 
 
 

日志

 
 
关于我

一个特立独行的Java程序员,比较宅,上上网,写博客,听音乐,看电影。

网易考拉推荐

dojo study -- dojo/request/xhr  

2013-11-18 18:19:30|  分类: Javascript |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
dojo/request/xhr 一个使用XMLHttpRequest对象发送异步请求的模块。
How to Use?
require(["dojo/request/xhr"], function(xhr){
  xhr("example.json", {
    handleAs: "json"
  }).then(function(data){
    // Do something with the handled data
  }, function(err){
    // Handle the error condition
  }, function(evt){
    // Handle a progress event from the request if the
    // browser supports XHR2
  });
});
xhr方法有俩个参数
url 要请求的url
option 配置object

option 选项:
  • data 用于post请求
  • handleAs 处理的数据类型 默认是text, 其他值 json, xml, javascript
  • method GET POST等必须大写
  • query query字符串,会追加到url后边
  • timeout 超时时间
  • preventCache Whether to append a cache-busting parameter to the URL. 会在url加上 request.preventCache=timestampGMT 
  • sync 是否同步,默认false
  • headers    { 'Content-Type': 'application/x-www-form-urlencoded' }

一个实例

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://zhenghaoju700.blog.163.com/blog/./dojo-release-1.9.1-src/dijit/themes/claro/claro.css">
<style type="text/css">
#header {
background:#ccc;
overflow:hidden; /* so we can animate to zero width */
width:600px;
}
</style>
<script type="text/javascript">
dojoConfig = {
async: true,
tlmSibingOfDojo: false,
parseOnLoad: true,
//isDebug: false,
locale: 'en-us',
baseUrl: "./dojo-release-1.9.1-src",
packages: [
{name: "dojo", location: "./dojo"},
{name: "dijit", location: "./dijit"},
{name: "dojox", location: "./dojox"}
]
};
</script>
<script src="http://zhenghaoju700.blog.163.com/blog/./dojo-release-1.9.1-src/dojo/dojo.js"></script>

<script>
require(["dojo/dom", "dojo/request/xhr", "dijit/form/Button", "dojo/json", "dojo/domReady!"], function(dom, xhr, Button, JSON){
var myButton = new Button({
label: "Click me to send request!",
onClick: function(){
// Do something:
var requestStr = "http://9.111.244.79:8080/webTest/getData.action";
var result = dom.byId("result");
xhr(requestStr, {
handleAs: "json",
method: "GET",
query: "name=hjzheng",
timeout: 1000,
headers: {'text': 'text'},
preventCache: true
}).then(function(data){
// Do something with the handled data
result.innerHTML = JSON.stringify(data);
}, function(err){
// Handle the error condition
result.innerHTML = "Server Error!!!";
}, function(evt){
// Handle a progress event from the request if the
// browser supports XHR2
});
}
}, "button");

});

</script>
</head>
<body class="claro">
<h2 id="header">dojo request</h2>
<div id="button"></div>
<h1 id="result"></h1>
</body>
</html>

dojo study -- dojo/request/xhr - 星期五 - 星期五
 
dojo/request/notify
用来监听整个request API的模块
支持监听的事件event
startNoneIn-flight requests have started
sendResponse, Cancel functionEmitted prior to a provider sending data; calling cancel prevents the request from being sent
loadResponseA request successfully completed
errorResponseA request failed
doneResponseA request has finished (regardless of success or failure)
stopNoneAll in-flight requests have finished
notify会返回一个带有remove()方法,调用它会删除该监听事件。
例子

require(["dojo/dom", "dojo/request/xhr", "dojo/request/notify", "dijit/form/Button", "dojo/json", "dojo/domReady!"], function(dom, xhr, notify, Button, JSON){
notify("start", function(){
console.info("xhr start")
});
notify("send", function(response, cancel){
// cancel() can be called to prevent the request from
// being sent
console.info("xhr send: " +JSON.stringify(response));
});
notify("load", function(response){
console.info("xhr load: " + JSON.stringify(response));
});
notify("error", function(response){
console.info("xhr error: " + JSON.stringify(response));
});
notify("done", function(response){
console.info("xhr done: " + JSON.stringify(response));
});
notify("stop", function(){
console.info("xhr stop");
});

var myButton = new Button({
label: "Click me to send request!",
onClick: function(){
// Do something:
var requestStr = "http://9.111.244.79:8080/webTestdojo/getData.action";
var result = dom.byId("result");
xhr(requestStr, {
handleAs: "json",
method: "GET",
query: "name=hjzheng",
timeout: 1000,
headers: {'text': 'text'},
preventCache: true
}).then(function(data){
// Do something with the handled data
result.innerHTML = JSON.stringify(data);
}, function(err){
// Handle the error condition
result.innerHTML = "Server Error!!!";
}, function(evt){
// Handle a progress event from the request if the
// browser supports XHR2
});
}
}, "button");

});

输出:
dojo study -- dojo/request/xhr - 星期五 - 星期五
 
参考资料:http://dojotoolkit.org/reference-guide/1.9/dojo/request/xhr.html
  评论这张
 
阅读(694)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017