Jquery's Ajax program is simple

Source: Internet
Author: User
An example of understanding Ajax, JSON, and ashx is that when you enter the user name, it is very simple to compare the request page. However, it allows beginners to better understand Ajax in query.

Javascript: Javascript Code
  1. <SCRIPT src = "JS/jquery-1.4.2.js" type = "text/JavaScript"> </SCRIPT>
  2. <SCRIPT type ="Text/JavaScript">
  3. Function alertmsg ()
  4. {
  5. VaR user = {"ID": "", "PWD ":""};
  6. User. ID = $ ("Input: Text"). Val ();
  7. User. Pwd = $ ("Input: Password"). Val ();
  8. $. Ajax ({
  9. URL:"Handleajax. ashx", // we recommend that you use ashx to process the request page.ProgramIt is equivalent to a lightweight Aspx. Here there is no need to use aspx for data processing, which makes a little contribution to performance.
  10. Data:"User =" + User. ID,
  11. Datatype:"JSON", // data type returned on the request page
  12. Type:"Get ",
  13. Contenttype:"Application/JSON", // note that the contenttype of the request page must be consistent here
  14. Success:Function (data) {// The data here is the data returned by the request page
  15. Alert (data. Result );
  16. },
  17. Error:Function (XMLHttpRequest, textstatus, errorthrown ){
  18. Alert ("XMLHttpRequest ");
  19. }
  20. });
  21. }
<SCRIPT src = "JS/jquery-1.4.2.js" type = "text/JavaScript"> </SCRIPT> <SCRIPT type = "text/JavaScript"> function alertmsg () {var user = {"ID": "", "PWD": ""}; user. id = $ ("input: Text "). val (); User. pwd = $ ("input: Password "). val (); $. ajax ({URL: "handleajax. ashx ", // we recommend that you use the ashx processing program on the request page. The general processing program is equivalent to a lightweight Aspx. There is no need to use aspx for data processing, it makes a little contribution to the performance. Data: "user =" + User. ID, datatype: "JSON", // type returned by the request page: "Get", contenttype: "application/JSON ", // note that the contenttype of the request page must be consistent here success: function (data) {// The data here is the data returned by the request page alert (data. result) ;}, error: function (XMLHttpRequest, textstatus, errorthrown) {alert ("XMLHttpRequest ");}});}

HTML: HTML code

    1. <Body>
    2. User name:<Input id = "ID" type = "text" name = "ID"/>
    3. Password:<Input id = "PWD" type = "password" name = "PWD"/>
    4. <Input id = "Submit" type = "button" name = "sub" value = "Submit" onclick = "alertmsg ();"/>
    5. </Body>
 
<Body> User name: <input id = "ID" type = "text" name = "ID"/> password: <input id = "PWD" type = "password" name = "PWD"/> <input id = "Submit" type = "button" name = "sub" value = "Submit "onclick =" alertmsg (); "/> </body>

Request page ashx C # code

  1. <% @ Webhandler Language = "C #" class = "handleajax" %>
  2. Using system;
  3. Using system. Web;
  4. Public class handleajax: ihttphandler {
  5. Public void processrequest (httpcontext context ){
  6. Context. response. contenttype ="Application/JSON ";
  7. If (context. request ["user"] = "JiaYou ")
  8. {
  9. String Hello = "OK ";
  10. Context. response. Write ("{\" Result \ ": \" "+ Hello +" \ "}"); // note the format "key ": "value" The format here is correct only after I have written the error several times
  11. }
  12. Else {context. response. Write ("{\" Result \ ": \" Hello pig \"");}
  13. }
  14. Public bool isreusable {
  15. Get {
  16. Return false;
  17. }
  18. }
  19. }

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.