关闭

dojo.io.bind使用详解

标签: functionxmlhttprequesturlpropertiesobjectcallback
3649人阅读 评论(0) 收藏 举报
分类:
访问一个纯文本资源:
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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:81647次
    • 积分:1033
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:15篇
    • 译文:0篇
    • 评论:8条
    文章分类
    最新评论