Summary of jquery, Ajax call backstage methods in ASP.

Source: Internet
Author: User
Throughout the process to learn a lot of knowledge points, understand the jquery, Ajax in the use of ASP, to summarize, in fact, the principle is the same, understand a kind of, other attention to a few differences can be, flexible use:

1. Method invocation with parameters

The sample code is as follows:
Front desk jquery Code:
$ (function () { 

One thing you know about Ajax is the way the background is tuned.

1. Method invocation with parameters

The sample code is as follows:

Front desk jquery Code:

<span style= "font-size:18px" >$ (function () {    var browers = Browersestimate ();    var params = ' {browerstype: ' + browers + ' "} ';    $.ajax ({        type: "POST",                   //Submit        by URL: "Default.aspx/recorddata",   //Submitted page/method name        Data:params,                   / /parameter (if no parameters: null)        DataType: "JSON",                   //Type        contentType: "Application/json; Charset=utf-8 ",             success:function (data) {                     ///////////Return of the DATA.D to get Content                       alert (DATA.D);                 },                 error: function (ERR) {                     alert (err);                });          </span>

This is the Ajax method called back-end method under jquery.

There are a few things to note about this approach:

The type must be post, and then the method that is behind it must be static, the method declaration to add the attribute [System.Web.Services.WebMethod ()], and the number of arguments passed should be the same as the parameters of the method.

Asp. NET Backend methods:

<span style= "font-size:18px" >  [System.Web.Services.WebMethod ()] public      static void RecordData (string Browerstype)      {          if (browsercontrol.counters = = null)          {              browsercontrol.initdata (0);          }            if (Browerstype = = "")          {              Browerstype = "other";          }            Browsercontrol.addonebybrowsertype (browerstype);          if (Browsercontrol.writeindatabase ())          {              browsercontrol.oldtotalcount = BrowserControl.Counters.Count;          }          Else          {              Browsercontrol.oldtotalcount = 0;          }      } </span>

2. Method calls without parameters

Example code:

Front desk jquery Code

<span style= "font-size:18px" >$ (function () {           $ ("#btnOK"). Click (function () {               $.ajax ({                   //) to post                   type: "Post",                   //method on which page and method name                   URL: "Data.aspx/sayhello",                   contentType: "Application/json; Charset=utf-8 ",                   dataType:" JSON ",                   success:function (data) {                       /////returned by DATA.D get content                       alert (DATA.D);                   } ,                   error:function (err) {                       alert (err);                   }               });               Disable the button's commit               return false;           })       ; </span>

ASP. Net Backend method

<span style= "font-size:18px" >[system.web.services.webmethod ()] public  static string SayHello ()       {       return "Hello ajax!";       } </span>

3. Call to return array method

Sample foreground jquery code:

<span style= "font-size:18px" >$ (function () {           $ ("#btnOK"). Click (function () {               $.ajax ({                   type: "Post" ,                   URL: "Data.aspx/getarray",                   contentType: "Application/json; Charset=utf-8 ",                   dataType:" JSON ",                   success:function (data) {                       //before inserting, empty the UL                       $ (" #list "). HTML (" ");                       Recursively gets the data                       $ (DATA.D). each (function () {                           //insert result into Li inside                           $ ("#list"). Append ("" + This + "");                       });                       alert (DATA.D);                   },                   error:function (err) {                       alert (err);                   }               });               Disable the button's commit               return false;           })       ; </span>

ASP. NET Spool code: ASP.

<span style= "font-size:18px" >[system.web.services.webmethod ()] public     static List GetArray ()       {         List li = new list ();       for (int i = 0; i <; i++)               Li. ADD (i + "");       return li;       } </span>

4. Manipulating XML

<span style= "font-size:18px" >xnl file Example:      <?xml version= "1.0" encoding= "Utf-8"?>      <data>        <item>          <id>1</id>          <name>qwe</name>        </item>        < item>          <id>2</id>          <name>asd</name>        </item>      </data>       jquery Code: jquery Code:</span>

jquery Front Code:

<span style= "font-size:18px" >$ (function () {$ ("#btnOK"). Click (function () {$.ajax ({url: "Xmltest.xml", DataType: ' xml ',//return type is XML, and the preceding JSON                        , not the same. Success:function (XML) {//Empties list $ ("#list"). HTML ("");                            Finds the XML element $ (XML). Find ("Data>item"). each (function () {                            $ ("#list"). Append ("ID:" + $ (this). FIND ("id"). Text () + "");                       $ ("#list"). Append ("Name:" + $ (this). Find ("name"). Text () + "");                       })}, Error:function (result, status) {///If there is an error above the capture, the callback function will be executed here                   alert (status);               }               });           Disables the button's commit return false;       }); }); </span> 

Summarize

Mainly we should pay attention to the JS code inside the method name to be consistent with the background, and then there is the background method must be static, method declaration to add the attribute [System.Web.Services.WebMethod ()], the number of parameters passed should be the same as the parameters of the method, Note that these parts of our debugging is much simpler, the rest is a lot of practice, strengthen the understanding of the principle.


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.