. Net Ajax asynchronous call implementation methods (jquery)

Source: Internet
Author: User

Different implementation methods of Ajax asynchronous calls in several. Net methods are introduced.

 

(1). aspx common web form page.

Delete the HTML code on the ASPX page and retain only the first line. Write the corresponding method code in the post code page. aspx. CS.

Foreground call:

// Asynchronously call the page url, parameter group, and return the execution event
$. Post ("ajaxpage. aspx", {type: 'getdata01'}, function (re ){
('{Re_method01'{.html (re );
});

(2) Separate. ASPX page ("place code in a separate file" is not checked during creation) + any. CS file (Inherit System. Web. UI. Page)

Code in the. aspx file <% @ page inherits = "namespace. Class" %>

(3). ashx page

The main knowledge point here is to inherit the ihttphandler interface. To handle HTTP web-related events.
Implementation Method: after creating the file, cancel context. response. Write ("Hello World"); change it to your event processing code.

The front-end Ajax call methods of 1, 2, and 3 are the same. If you need to distinguish between different Ajax calls. We can pass a type parameter with different values. The background then runs the respective processing programs through the switch.

(4) use system. Web. Services. webmethodattribute.

REFERENCE The namespace using system. Web. Services on the basis of (1); then add the [webmethod] attribute on the method to be executed asynchronously.

    [WebMethod]
public static string HandleEvent01(string para,string para2)
{
//code...
return "YOUR DATA";
}

Front-End call code:

$. Ajax ({
Type: "Post ",
Contenttype: "application/JSON ",
URL: "ajaxwebservice. aspx/handleevent01", // address and method name combination for WebService call ---- wsurl/method name
Data: "{'para': 'Bill ', 'para2': '28'}", // here is the parameter to be passed, note that the parameter name corresponds to the parameter name of the background method.
Datatype: 'json', // WebService returns the JSON type or JSON/string
Success: function (re ){
('Your re_method04'your .html (Re. D );
}
});

You can return JSON or string. The returned data format is {"D": Your Data }. That's why we set the value at the front-end to re. D.

Generally, the returned data type can be a common string, custom object, and generic list. Let's take a look at the front-end JSON data format when different types of data are returned.

Background code:

[Webmethod]
Public static string handleevent01 ()
{
// Code...
Return "Some Words ";
}

[Webmethod]
Public static person ajaxmethodthree (string para)
{
Return new person {name = "bill", Country = "China", favor = new string [] {"box", "Music", "football "}};
}

[Webmethod]
Public static list <person> ajaxmethodtwo (string para)
{
List <person> personlist = new list <person> ();
Personlist. add (new person {name = "bill", Country = "China", favor = new string [] {"box1", "film1", "football1 "}});
Personlist. add (new person {name = "Tom", Country = "USA", favor = new string [] {"box2", "music2", "football2 "}});
Personlist. add (new person {name = "Lucy", Country = "Korea", favor = new string [] {"box3", "music3", "football3 "}});
Return personlist;
}

// Define a person object
Public class person
{
Public string name {Get; set ;}
Public String country {Get; set ;}
Public String [] favor {Get; set ;}
}

1. Return string
JSON: {"D": "string "}
2. The return type is a custom object, and the return value is a corresponding JSON object.
JSON: {"D": {"name1": "value1", "name2": "value2 "...}}

3. The returned type is the generic list, and the returned value is the corresponding JSON object array.

If we need to return JSON data asynchronously, It is very convenient. In addition to being called using WebService, this page can also be accessed asynchronously using a normal URL, that is, (1.

(5) Call WebService

Add a web service. A webservicexxx. asmx and webservicexxx. cs. asynchronous program will be generated and implemented in webservicexxx. CS, which is similar to (4.

 

(6) asynchronous Ajax implementation in MVC

1. directly write the Public String Ajax () {return "re";} method in the controller without creating an additional View File.
2. or the structure of the content you want to return is still complicated. Create a _ Ajax. cshtml segment view. Controller code:

        public ActionResult Ajax()
{
    //...your code...
return PartialView("_Ajax");
}

Front-End call code:

$. Post ("/controller name/ajax", {type: 'getdata01'}, function (re ){
('{Re_method01'{.html (re );
});

The above code is compiled into a project for code download.

 

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.