Learning Ajax framework-Dojo: Section 3-XMLHTTP usage in Dojo (with source code)

Source: Internet
Author: User
Tags tidy

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.