In section 2, we introduced a simple Dom operation method. Next we should go to the traditional Ajax project-XMLHttpRequest. When using XMLHttpRequest, you need to pay attention to the encoding problem. What should I do if I want to bind dojo to UTF-8 by default? It's easy. You just need to modify the label when dojo. JS is introduced:
<SCRIPT type = "text/JavaScript" src = ". /Dojo-lib/dojo. JS "djconfig =" isdebug: True, bindencoding: 'utf-8' "> </SCRIPT>
Next, we will use an example to illustrate how to use XMLHttpRequest in Dojo.
Instance 1:
Function:Simply call the content of the JSP page without passing parameters.
The source code is as follows:
HTML page, that is, Home Page
Method 1:
<HTML>
<Head>
<Meta name = "generator" content = "HTML tidy, see http://www.w3.org/">
<Meta http-equiv = "Content-Type" content = "text/html; charset = iso-8859-1">
<Title> dojo, XMLHttpRequest! </Title>
<SCRIPT type = "text/JavaScript" src ="Dojoroot/dojo. js"Djconfig =" isdebug: True, bindencoding: 'utf-8' ">
</SCRIPT>
</Head>
<Body>
<Input id = "hello" value = "hello" type = "button"/> <input id = "dropcontent" value = "this is my first page! "Type =" text "/>
<Div id = "divhello">
</Div>
<SCRIPT type = "text/JavaScript">
Function responsevalue (responsetext){
Alert (responsetext );
Dojo. byid ("dropcontent"). value = responsetext;
}
Function responseerror (response){
Alert ("error ");
}
Function helloworld (){
Dojo. xhrget({
URL: "test. jsp ",
Method: "Get ",
Handleas: "text ",
Load: responsevalue,
Error: responseerror,
});
}
VaR first = dojo. byid ("hello ");
Dojo. Connect(First, "onclick", helloworld );
</SCRIPT>
</Body>
</Html>
Method 2:
<HTML>
<Head>
<Meta name = "generator" content = "HTML tidy, see http://www.w3.org/">
<Meta http-equiv = "Content-Type" content = "text/html; charset = iso-8859-1">
<Title> dojo, XMLHttpRequest! </Title>
<SCRIPT type = "text/JavaScript" src ="Dojo-0.3.1-ajax/dojo. js"Djconfig =" isdebug: True, bindencoding: 'utf-8' ">
</SCRIPT>
</Head>
<Body>
<Input id = "hello" value = "hello" type = "button"/> <input id = "dropcontent" value = "this is my first page! "Type =" text "/>
<Div id = "divhello">
</Div>
<SCRIPT type = "text/JavaScript">
Function helloworld1 (){
Dojo. Io. Bind({
URL: "test. jsp ",
Load:Function (type, Data, EVT){
Dojo. byid ("divhello"). innerhtml = data;
},
Error:Function (type, error){
Alert (error );
}
});
}
VaR first = dojo. byid ("hello ");
Dojo. event. Connect(First, "onclick", helloworld1 );
</SCRIPT>
</Body>
</Html>
Test. jsp page:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<%
String result = "Hello, dojo, XMLHttpRequest !";
Out. Print (result );
%>
Output result:Run the HTML page and click "hello". The output result is: Hello, dojo, XMLHttpRequest!
Example 2:
Function:Call the content of the JSP page and how to pass parameters. Add parameters in instance 1 codeContentYou can.
The source code is as follows:
HTML page:
VaRParams= {
Username: 'Feifei ',
ID: '123 ',
}
Function helloworld (){
Dojo. xhrget ({
URL: "testparams. jsp ",
Content: Params,
Method: "Get ",
Handleas: "text ",
Load: responsevalue,
Error: responseerror,
});
}
Testparams. jsp page:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<%
String username =Request. getparameter ("username ");
String Password =Request. getparameter ("ID ");
String result = username;
Result + = password;
Out. Print (result );
%>
Output result:Run the HTML page and click "hello". The output is feifei20022458.