Dojo-Ajax framework practice

Source: Internet
Author: User

Xhrget is the most important function in the xhr framework and has the highest usage frequency. By using this function, you can request static text resources such as TXT and XML on the server, or obtain dynamic pages such as PHP, JSP, and ASP, as long as the character data stream is returned from the server.

 

In addition to xhrget, The xhr framework of dojo also includes xhrpost, rawxhrpost, xhrput, rawxhrput, and xhrdelete. These functions are similar to xhrget. For methods and parameters, see xhrget. The difference is their HTTP request type. xhrpost sends POST requests, xhrput sends put requests, and xhrdelete
The Delete request occurs.

 

Let's look at several instances:

 

1. Use xhrget to request text resources

Client --

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "hellodojoajax. aspx. cs" inherits = "dojotest. hellodojoajax" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

Server resources --

hello world!!Dojo!

 

 

2. Use xhrget to obtain JSON data

Client --

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "dojoajaxjson. aspx. cs" inherits = "dojotest. dojoajaxjson" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

 

Server --

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; namespace jqueryajaxtest. data {public partial class getcity: system. web. UI. page {protected void page_load (Object sender, eventargs e) {string result = @ "[{" "provinceid" ":" "BJ" "," "cityname "": "" Beijing "" },{ "" provinceid "": "" TJ "", "" cityname ":" "Tianjin" "}]"; // clear the Buffer Response. clear (); // write the string to the response output stream response. write (result); // send the Current Buffer output to the client, and stop this page to execute response. end ();}}}

 

3. Use xhrget to submit a form

Client --

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "dojoajaxtext. aspx. cs" inherits = "dojotest. dojoajaxtext" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

Server --

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; namespace dojotest {public partial class getservice: system. web. UI. page {protected void page_load (Object sender, eventargs e) {string id = ""; string Pwd = ""; // get the parameter if (! String. isnullorempty (httpcontext. Current. request ["userid"]) &! String. isnullorempty (httpcontext. current. request ["password"]) {id = httpcontext. current. request ["userid"]; Pwd = httpcontext. current. request ["password"];} // clear the Buffer Response. clear (); // write the string to the response output stream response. write ("user input ID:" + ID + ", input password:" + PWD); // the client that sends all the current buffered outputs, and stop this page to execute response. end ();}}}

 

Note:

1,

The callback function printresult contains two parameters: Response and ioargs.

Response: indicates the data returned from the server. dojo has pre-processed the data type set by handleas.

Ioargs: This is an object that contains some parameters used to call xhrget. The reason why I put this information in an object and pass it to the callback function is to give the callback function an execution "context" to let the callback function know which HTTP request it belongs to and which parameters the request has, the type of the returned data. This information is particularly useful when debugging programs.

Ioargs. url: the requested URL, which is the same as the value set when xhrget is called.

Ioargs. Query: The parameter contained in the request. .

Ioargs. handas: How to pre-process the returned data, which is the same as the value set when xhrget is called.

Ioargs. xhr: The xhr object used by the xhrget function.

 

2. handleas (preprocessing method)

Text: the default value. It does not process the returned data.

XML: returns the responsexml of the xhr object.

Javascript: Use dojo. eval to process the returned data and return the processing result.

JSON: Use dojo. fromjson to process the returned data and return the generated JSON object.

JSON-comment-optional: if data is included in the annotator, only dojo. fromjson processes this part of data. If no data is included in the annotator, use dojo. fromjson processes all data.

JSON-comment-filtered: data should be included in /*... */, Return the JSON object generated using dojo. fromjson. If the data is not included in the annotator, It is not processed.

 

3. Comments in the Code also illustrate some noteworthy points.

 

 

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.