dojo.io.bind使用详解

访问一个纯文本资源:
dojo.io.bind({
    url: "http://foo.bar.com/sampleData.txt",
    load: function(type, data, evt){ /*do something w/ the data */ },
    error: function(type, error){ /*do something w/ the error*/ },
    mimetype: "text/plain"
});



dojo.io.bind({
    url: "http://foo.bar.com/sampleData.js",
    load: function(type, evaldObj){ /* do something */ },
    mimetype: "text/javas cript"
});

提交Form:

dojo.io.bind({
    url: "http://foo.bar.com/processForm.cgi",
    load: function(type, evaldObj){ /* do something */ },
    formNode: dojo.byId("formToSubmit")
});

参数:
加载一段
url:
the location of the resource being requested
mimetype:
Mimetype used to interpret the contents of the response with. Defaults to "text/plain". This does not set an outgoing mime header for HTTP transports.
method:
Format to use whem making the request. For HTTP transports this us usually either of "GET" or "POST" although non-HTTP transports may define and accept others. Defaults to "GET".
content:
An key/value mapping of properties to be constructed into paramaters passed with the data request. In HTTP transports, these are equivalent to query string paramaters or form fields in POST requests. The exact behavior of content-specified paramaters is dependent upon the transport in use. the value format should like {key1:value1,key2:value2}
load:
                         成功回调函数
error:
                         错误回调函数

transport:
String that explicitly specifies the transport object to use for the request. If the specified transport is not available, the request will not be made and the error callback will be fired.
changeUrl:
Boolean, defaults to false. Determines whether or not the request should be made "bookmarkable". This may be removed in the future as it pertains exclusively to in-browser HTTP transports.
formNode:
DOM Node that specifies a form to be serialized and submitted by this request. Form nodes may be used to populate the method and url properties by some transports. This property may be removed in the future as it pertains exclusively to in-browser HTTP transports.
sync:
Boolean, defaults to false. sync determines whether the accepting transport should attempt to make the request synchronously. Transports that support synchronous operation will block execution of other code until the bind is complete.
bindSuccess:
Boolean, defaults to false. Indicates whether or not this Request was accepted and dispatched by any transport.
useCache:
Boolean, defaults to false. Indicates whether the result of the request should be cached and whether requesting a result for this request from the cache is acceptable.
timeout:
                         请求超时时间
 

原文:http://blog.chinaunix.net/u/8780/showart_349663.html


dojo.io包的大多数魔力通过bind()展现。dojo.io.bind() 提供了通用的异步请求API。

我们可以使用以下的代码从一个URL地址中请求获取原始文本:

 

dojo.io.bind({ url: "http://foo.bar.com/sampleData.txt", load: function(type, data, evt){ /*do something w/ the data */ }, mimetype: "text/plain"});

以上的代码即全部的实现。通过代码我们已经提供了获取数据的地址,获取了数据后的回调函数。不过,当请求出错时,我们需要提供错误处理函数:

 

dojo.io.bind({ url: "http://foo.bar.com/sampleData.txt", load: function(type, data, evt){ /*do something w/ the data */ }, error: function(type, error){ /*do something w/ the error*/ }, mimetype: "text/plain"});

当然,只注册单一的处理函数也是可以的,在这个处理函数中需要指明传递的事件类型进行相应的处理,用这种方式取代对load和error处理函数的分别注册:

 

dojo.io.bind({ url: "http://foo.bar.com/sampleData.txt", handle: function(type, data, evt){ if(type == "load"){ // do something with the data object }else if(type == "error"){ // here, "data" is our error object // respond to the error here }else{ // other types of events might get passed, handle them here } }, mimetype: "text/plain"});

出于性能的考虑,我们经常使用的一种动态加载内容的做法是请求java script语法的字符串,然后进行eval操作。bind方法也同时包含了这种处理操作,我们只需要设置mimetype参数,提供希望获取的响应类型:

 

dojo.io.bind({ url: "http://foo.bar.com/sampleData.js", load: function(type, evaldObj){ /* do something */ }, mimetype: "text/java script"});

当然,如果我们希望确保使用的是XMLHttpRequest传输对象,可以采用如下方式指定:

 

dojo.io.bind({ url: "http://foo.bar.com/sampleData.js", load: function(type, evaldObj){ /* do something */ }, mimetype: "text/plain", // get plain text, don't eval() transport: "XMLHTTPTransport"});

除了以上提供的全面的功能外,bind()方法同时还支持通过请求的方式提交表单(注意:这里不支持通过XMLHTTP上传文件):

 

dojo.io.bind({ url: "http://foo.bar.com/processForm.cgi", load: function(type, evaldObj){ /* do something */ }, formNode: document.getElementById("formToSubmit")});

原文:http://blog.163.com/yangtao_0329/blog/static/2359758920073410025211/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值