Ajax syntax JSON calls ASPX page is different from MVC page

Source: Internet
Author: User

Http://www.cnblogs.com/fire-phoenix/archive/2009/11/13/1614146.html

This article describes how to use jquery-based Ajax technology in ASP. NET (ASP. NET/ajax. (For source code download, see final)

Before using jquery, go to www.jquery.com to download the latest JS Code.

<SCRIPT src = "_ scripts/jQuery-1.3.2.js" type = "text/javasScripts "> </SCRIPT>You can,
Of course, Microsoft has integrated jquery into vs, so you can view it at www. ASP. NET/Ajax.

This article describes how to use jquery in ASP. NET using six columns. The layout of this project is as follows:


Sample1: jquery supports get-based Ajax

This example shows how to use the jquery-based get method in ASP. NET. The page layout code is as follows:

The page layout is very simple. A text box is used to enter the number of votes, and a div with ID error1 is used to display the Ajax operation result.
The following describes the Ajax method.

Before learning the code, let's take a look at the running results:
Running Effect


Enter a number after the number of votes. If the number is greater than 5, a prompt is displayed. Otherwise, no. The change event of the text box is used here, so after entering a number, you need to disable the focus,
Otherwise, the result is not displayed.
The following is a simple explanation of the above Code:


<Script language = "javasOther ">

$ (Document). Ready (function (){

...

}


</SCRIPT>

It is a standard sentence similar to English usage. When the page is complete, it means doing something.
Someone may need to ask why such a type is needed. This is because JS has different locations and requires different judgments. Consider the Baidu page you opened. When the Baidu page is loaded, the cursor will automatically locate the input box.

The code here may be similar to the following:

<Input type = "text" id = "F">

<SCRIPT>

VaR o = Document. getelemementbyid ("F ");

O. Focus ();
</SCRIPT>

Here, this script must be followed.

<SCRIPT>

VaR o = Document. getelemementbyid ("F ");

</SCRIPT>

<Input type = "text" id = "F">

The browser reports an error during execution, so the text box is not loaded yet. You don't have to worry about jquery.
$ (Document). Ready (function () {}, which is also a benefit of jquery.

Others are quite simple. In summary, for input boxes such as textbox and select, use Val () to get/set the value. For Div, P, span, etc., it is HTML () GET/set its value,
$. Get ("getticks. aspx", function (result ){...} Indicates that the data processing will be executed by getticks. aspx,

Getticks. aspx is simple, as follows:

 
Int noofticketsavailable = 5;

Response. Write (noofticketsavailable. tostring ());

Response. End ();

Note: Response. End ();

This is OK.

Sample2: jquery supports post-based Ajax

Not much different from sample1

The Code is as follows:

However, you must note that parameters are passed here.TicreqFor URLs, names are generally key-based. For example
Default. aspx? You can use request. querystring ["ID"] to obtain the value of Id 1.
Getticksbypost. aspx? Ticreq = ticketsreq (however, Here ticketsreq is a variable value and the system automatically converts it)
Therefore, this parameter can be obtained in getticksbypost. aspx.


Same as running result

Sample3: jquery supports ASP. NET-based
Ajax

To use jquery in ASP. NET Ajax, set enablepagemethods to true. As follows:

Next, you can use ASP. NET ajajx technology, as shown below:


Then use the URL. Note the URL format, followed by the method.

URL: "default. aspx/getavailabletickets ",The getavailabletickets method defined in default. aspx is used.
The background code is as follows,


Note: you must add a webmethod modifier.

Sample3: jquery supports parameters passing through Ajax Based on ASP. NET Ajax.


The following is the main code.The

The NO parameter is passed, so it can be directly used in the background.


Note: The parameter names in the day after tomorrow must be the same as those defined in Ajax at the front end.


Sample4: jquery supports ASP. NET-based
Ajax transmits parameters to WebService

To use WebService, you must add

[System. Web. Script. Services. scriptservice]

See the following code (in the default. js file)

The following is a WebService file. Pay attention to the red mark.


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.