Module: dojo. Io. Io
Dojo. Io. Bind
Process the request to retrieve and process the required data
This function is the most important and useful function in Ajax. io. the BIND class is used to process communication between the client and the server. The parameters to be communicated are defined by the object dojo. io. request. The specific communication method is provided by another object transport.
Therefore, if we need to communicate with the server, we should define a request object, including the server address and callback function. In this example, requset is defined as an anonymous object.
Although we can define a transport of our own, it is obviously easier to directly use the ready-made transport.
Dojo provides a dojo that is compatible with both IE and Firefox. io. xmlhttptransport, but this object is located in Dojo. io. browserio. Therefore, require dojo. io. in Io, require dojo. io. browserio
Usage example:
Dojo. Io. BIND ({
URL: "http: // localhost/test.html", // the address of the page to be requested
Mimetype: "text/html", // The type of the requested page. It should be set to mimetype corresponding to the type of the requested page. The default value is "text/plain"
Method: "Get", // The default value is "get"
Sync: false, // asynchronous execution by default
Usecache: false, // by default, the page cache is not used. Note that the cache here is not the browser cache, but the page cache maintained by dojo itself.
Preventcache: false, // the browser cache is enabled by default. Otherwise, different parameters are automatically added to ensure that the browser cache is invalid.
Timeoutseconds: 3000, // timeout after 3 seconds. If it is 0, it never times out.
Load: function (type, Data, EVT) {alert (data) ;}, // type shocould be "LOAD", data is that we wanted
Error: function (type, error) {alert (error. Message) ;}, // error is Dojo. Io. Error
Timeout: function (type) {alert ("request timeout! ");}
});
You can also use a handle to handle all events.
dojo. io. BIND ({
URL: "http: // localhost/test.html", // address of the page to be requested
mimetype: "text/html ", // The requested page type, which should be set to mimetype corresponding to the page type you requested
timeoutseconds: 3000, // timeout after 3 seconds, if it is 0, it never times out.
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 nee D handled
}< BR >});
If the transport used is not specified in the request, dojo automatically searches for the transport that can process the request in the registered transports. If the request cannot be found, the specified request is returned. The following is an example with transport specified:
Dojo. Io. BIND ({
URL: "http: // localhost/test.html", // the address of the page to be requested
Mimetype: "text/html", // type of the requested page, which should be set to the mimetype corresponding to the type of the requested page
Timeoutseconds: 3000, // timeout after 3 seconds. If it is 0, it never times out.
Transport: "xmlhttptransport ",
Load: function (type, Data, EVT) {alert (data) ;}, // type shocould be "LOAD", data is that we wanted
Error: function (type, error) {alert (error. Message) ;}, // error is Dojo. Io. Error
Timeout: function (type) {alert ("request timeout! ");}
});
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 defines an object
Mimetype: "text/JavaScript", // type of the requested page, which should be set to the mimetype corresponding to the type of the requested page
Timeoutseconds: 3000, // timeout after 3 seconds. If it is 0, it never times out.
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 post:
Dojo. Io. BIND ({
URL: "http: // localhost/test. aspx", // address of the page to be submitted
Mimetype: "text/html", // type of the requested page, which should be set to the mimetype corresponding to the type of the requested page
Timeoutseconds: 3000, // timeout after 3 seconds. If it is 0, it never times out.
Method: "Post ",
Formnode: dojo. byid ("myform"), // specify the name of the submitted Form
Load: function (type, Data, EVT) {alert (data) ;}, // type shocould be "LOAD", data is that we wanted
Error: function (type, error) {alert (error. Message) ;}, // error is Dojo. Io. Error
Timeout: function (type) {alert ("request timeout! ");}
});
Example of another post (without form to post ):
Dojo. Io. BIND ({
URL: "http: // localhost/test. aspx", // address of the page to be submitted
Mimetype: "text/html", // type of the requested page, which should be set to the mimetype corresponding to the type of the requested page
Timeoutseconds: 3000, // timeout after 3 seconds. If it is 0, it never times out.
Method: "Post ",
Content: {A: 1, B: 2}, // data to be submitted
Load: function (type, Data, EVT) {alert (data) ;}, // type shocould be "LOAD", data is that we wanted
Error: function (type, error) {alert (error. Message) ;}, // error is Dojo. Io. Error
Timeout: function (type) {alert ("request timeout! ");}
});
Dojo. Io. queuebind
Sometimes, if we need to send multiple webpage requests at a time, we should use dojo. io. queuebind, because the browser may only allow sending a limited number of requests at the same time. io. BIND may fail to apply for a new XMLHTTP object, resulting in an error.
The usage is the same as that of dojo. Io. Bind.
Dojo. Io. argsfrommap
The parameter format used to convert an object to a URL
Usage example:
Dojo. Io. argsfrommap ({A: 1, B: 2, C: 3}); // will return "c = 3 & B = 2 & A = 1"
Dojo. io. argsfrommap ({name: "name", value: "value"}, "UTF "); // will return "value = % E5 % 80% BC & name = % E5 % 90% 8d % E7 % A7 % B0". If you have any Chinese characters, specify the UTF format. Otherwise, dojo. string. the encoding returned by encodeascii is weird.
Dojo. io. argsfrommap ({A: 1, B: 2, C: 3}, "UTF", "C "); // will return "B = 2 & A = 1 & C = 3". The last parameter can control that the value of the specified name appears at the end.
Dojo. Io. setiframesrc
Set the SRC of IFRAME
Usage example:
Dojo. Io. setiframesrc (Dojo. byid ("myframe"), "http: // localhost/test.htm"); // myframe
Dojo. io. setiframesrc (Dojo. byid ("myframe"), "http: // localhost/test.htm", true); // myframe open the specified webpage and overwrite the browser's historical records
Todo
Module: dojo. Io. browserio
Basically, the dojo. Io. xmlhttptransport object is provided.
Xmlhttptransport can meet our needs, but it has several restrictions: it cannot transfer files, it cannot successfully execute cross-domain remote requests, and does not support protocols such as file: //
Therefore, based on application requirements, we may need to choose other transport: dojo. Io. iframetransport, dojo. Io. repubsubtranport, dojo. Io. scriptsrctransport, and javasbustransport.
Dojo. Io. iframetransport is used in the same way as XMLHTTP. Its advantage is that it can be cross-origin and does not have any security issues.
If the mimetype specified by the request is text or Javascript, the returned content should be placed in the first textarea. If the specified mimetype is HTML, the HTML in IFRAME is the required content. Because of browser compatibility, iframetransport cannot correctly process requests with the returned type of XML.
For RPC, this is similar to remoting and will be introduced later.