Module: Dojo.io.IO
Dojo.io.bind
Process requests to retrieve the required data and process
This function is the most important and useful function in Ajax, Dojo.io.bind This class is used to handle the communication between the client and the server, the parameters that need to communicate are defined by object Dojo.io.Request, and the method of communication is provided by another object transport.
Therefore, if we need to communicate with the server, we should define a request object, which includes the server address and callback functions, which are defined as anonymous objects in the example Requset
Although we can define one's own transport, it is obviously not as convenient as using ready-made transport directly.
Dojo provides a dojo.io.XMLHTTPTransport that is compatible with both IE and Firefox, but this object is located in Dojo.io.BrowserIO, so it is common require Dojo.io.IO to require Dojo.io.BrowserIO
Usage Example:
dojo.io.bind({
url: "http://localhost/test.html", //要请求的页面地址
mimetype: "text/html", //请求的页面的类型,应该设置为与你请求页面类型对应的mimetype,默认为 "text/plain"
method:"GET", //默认为"GET"
sync: false, //默认为异步执行
useCache: false, //默认为不使用页面缓存,注意这里的缓存并不是浏览器的缓存,而是Dojo自身所维护的页面缓存
preventCache: false, //默认为启用浏览器缓存,否则将通过自动增加不同的参数来确保浏览器缓存失效
timeoutSeconds: 3000, //3秒后超时,如果为0则永不超时
load: function(type, data, evt) { alert(data); }, //type should be "load", data is that we wanted
error: function(type, error) { alert(error.message); }, //error is dojo.io.Error
timeout: function(type) { alert("请求超时!"); }
});
You can also use a handle to handle all the events.
dojo.io.bind({
url: "http://localhost/test.html", //要请求的页面地址
mimetype: "text/html", //请求的页面的类型,应该设置为与你请求页面类型对应的mimetype
timeoutSeconds: 3000, //3秒后超时,如果为0则永不超时
handle: function(type, data, evt){
if(type == "load") { alert(data); } //data is that we wanted
else if (type == "error") { alert(data.message); } //data is the error object
else { ; } //other events maybe need handled
}
});
If you do not specify the transport used in the request, Dojo automatically looks for the transport in the registered transports to handle the request, and returns the specified request if it cannot be found. The following is an example that specifies the transport:
dojo.io.bind({
url: "http://localhost/test.html", //要请求的页面地址
mimetype: "text/html", //请求的页面的类型,应该设置为与你请求页面类型对应的mimetype
timeoutSeconds: 3000, //3秒后超时,如果为0则永不超时
transport: "XMLHTTPTransport",
load: function(type, data, evt) { alert(data); }, //type should be "load", data is that we wanted
error: function(type, error) { alert(error.message); }, //error is dojo.io.Error
timeout: function(type) { alert("请求超时!"); }
});
You can also use bind to get an object defined by JavaScript (note that mimetype must be defined as "Text/javascript")
testObj = dojo.io.bind({
url: "http://localhost/test.js", //test.js里定义了一个对象
mimetype: "text/javascript", //请求的页面的类型,应该设置为与你请求页面类型对应的mimetype
timeoutSeconds: 3000, //3秒后超时,如果为0则永不超时
handle: function(type, data, evt){
if(type == "load") { alert(data); } //data is a object or value
else if (type == "error") { alert(data.message); } //data is the error object
else { ; } //other events maybe need handled
}
});
The following is an example of a post:
dojo.io.bind({
url: "http://localhost/test.aspx", //要提交的页面地址
mimetype: "text/html", //请求的页面的类型,应该设置为与你请求页面类型对应的mimetype
timeoutSeconds: 3000, //3秒后超时,如果为0则永不超时
method: "POST",
formNode: dojo.byId("myForm"), //指定提交的Form名称
load: function(type, data, evt) { alert(data); }, //type should be "load", data is that we wanted
error: function(type, error) { alert(error.message); }, //error is dojo.io.Error
timeout: function(type) { alert("请求超时!"); }
});