The jquery call page background method is as follows:
Gives two simple examples, parameterless and parametric, and returns JSON data.
- <%@ Page language= "C #" autoeventwireup= true "codebehind=" JqueryCSMethodForm.aspx.cs "inherits=" Jquerweb.jquerycsmethodform "%>
- <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
- <title></title>
- <script type= "Text/javascript" src= "Js/jquery-1.4.2.js" ></script>
- <script type= "Text/javascript" >
- $ (document). Ready (function () {
- $ ("input[type= ' button '][value= ' GetDate ']"). Click (function () {
- $.ajax ({
- Type: "Post",
- URL: "Jquerycsmethodform.aspx/getnowdate",
- DataType: "JSON",
- ContentType: "Application/json; Charset=utf-8 ",
- Success:function (data) {
- $ ("Input#showtime"). Val (eval (' + DATA.D + ') ') [0].nowtime);
- },
- Error:function (XMLHttpRequest, Textstatus, Errorthrown) {
- alert (Errorthrown);
- }
- });
- });
- $ ("input[type= ' button '][value= ' Getonedaylater ']"). Click (function () {
- $.ajax ({
- Type: "Post",
- URL: "Jquerycsmethodform.aspx/getonedaylate",
- Data: "{days:1}",
- DataType: "JSON",
- ContentType: "Application/json; Charset=utf-8 ",
- Success:function (data) {
- $ ("Input#showtime"). Val (eval (' + DATA.D + ') ') [0].nowtime);
- },
- Error:function (XMLHttpRequest, Textstatus, Errorthrown) {
- alert (Errorthrown);
- }
- });
- });
- });
- </script>
- <body>
- <form id= "Form1" runat= "Server" >
- <div>
- <input type= "button" value= "GetDate"/>
- <input Type=button value= "Getonedaylater"/>
- <input type= "text" id= "ShowTime"/>
- </div>
- </form>
- </body>
CS Code:
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Web;
- Using System.Web.UI;
- Using System.Web.UI.WebControls;
- Using System.Web.Services;
- Namespace Jquerweb
- {
- public partial class JqueryCSMethodForm:System.Web.UI.Page
- {
- protected void Page_Load (object sender, EventArgs e)
- {
- }
- [WebMethod]
- public static String Getnowdate ()
- {
- Return "[{\" nowtime\ ": \" "+ DateTime.Now.ToShortDateString () +" \ "}]";
- }
- [WebMethod]
- public static String getonedaylate (Int32 days)
- {
- Return "[{\ nowtime\]: \" "+ DateTime.Now.AddDays (days). ToShortDateString () + "\"}];
- }
- }
- }
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"
- February 2011 13 jquery Best Plug-in recommendations
- Write a failed jquery optimization attempt
- JQuery 1.5 Official release five major changes compelling
- Dynamic adding and statistics of tabular data by jquery
- Simplifying Ajax development with JQuery
"Responsible editor: Chen Yu new TEL: (010) 68476606"