jquery Asynchronous Call page Background instance analysis

Source: Internet
Author: User
Tags datetime eval getdate json return string

The jquery call page background method is as follows:

Gives two simple examples, parameterless and parametric, and returns JSON data.

 
 
  1. <%@ Page language= "C #" autoeventwireup= true "codebehind=" JqueryCSMethodForm.aspx.cs "inherits=" Jquerweb.jquerycsmethodform "%>
  2. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  3. <title></title>
  4. <script type= "Text/javascript" src= "Js/jquery-1.4.2.js" ></script>
  5. <script type= "Text/javascript" >
  6. $ (document). Ready (function () {
  7. $ ("input[type= ' button '][value= ' GetDate ']"). Click (function () {
  8. $.ajax ({
  9. Type: "Post",
  10. URL: "Jquerycsmethodform.aspx/getnowdate",
  11. DataType: "JSON",
  12. ContentType: "Application/json; Charset=utf-8 ",
  13. Success:function (data) {
  14. $ ("Input#showtime"). Val (eval (' + DATA.D + ') ') [0].nowtime);
  15. },
  16. Error:function (XMLHttpRequest, Textstatus, Errorthrown) {
  17. alert (Errorthrown);
  18. }
  19. });
  20. });
  21. $ ("input[type= ' button '][value= ' Getonedaylater ']"). Click (function () {
  22. $.ajax ({
  23. Type: "Post",
  24. URL: "Jquerycsmethodform.aspx/getonedaylate",
  25. Data: "{days:1}",
  26. DataType: "JSON",
  27. ContentType: "Application/json; Charset=utf-8 ",
  28. Success:function (data) {
  29. $ ("Input#showtime"). Val (eval (' + DATA.D + ') ') [0].nowtime);
  30. },
  31. Error:function (XMLHttpRequest, Textstatus, Errorthrown) {
  32. alert (Errorthrown);
  33. }
  34. });
  35. });
  36. });
  37. </script>
  38. <body>
  39. <form id= "Form1" runat= "Server" >
  40. <div>
  41. <input type= "button" value= "GetDate"/>
  42. <input Type=button value= "Getonedaylater"/>
  43. <input type= "text" id= "ShowTime"/>
  44. </div>
  45. </form>
  46. </body>

CS Code:

 
 
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Web;
  5. Using System.Web.UI;
  6. Using System.Web.UI.WebControls;
  7. Using System.Web.Services;
  8. Namespace Jquerweb
  9. {
  10. public partial class JqueryCSMethodForm:System.Web.UI.Page
  11. {
  12. protected void Page_Load (object sender, EventArgs e)
  13. {
  14. }
  15. [WebMethod]
  16. public static String Getnowdate ()
  17. {
  18. Return "[{\" nowtime\ ": \" "+ DateTime.Now.ToShortDateString () +" \ "}]";
  19. }
  20. [WebMethod]
  21. public static String getonedaylate (Int32 days)
  22. {
  23. Return "[{\ nowtime\]: \" "+ DateTime.Now.AddDays (days). ToShortDateString () + "\"}];
  24. }
  25. }
  26. }

Note the point:

(1) The filling format of the URL url+ "/method name"

(2) ContentType: "Application/json; Charset=utf-8 ", this must have

(3) The type of the returned data is JSON

(4) Data: "{days:1}", transfer of parameters

(5) The method in the background must be public static and also have [WebMethod] attribute modification

Original link: http://www.cnblogs.com/liyuxin/archive/2011/03/01/1967776.html

"Edit Recommendation"

    1. February 2011 13 jquery Best Plug-in recommendations
    2. Write a failed jquery optimization attempt
    3. JQuery 1.5 Official release five major changes compelling
    4. Dynamic adding and statistics of tabular data by jquery
    5. Simplifying Ajax development with JQuery
"Responsible editor: Chen Yu new TEL: (010) 68476606"


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.