In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use Ajax in ASP.NET", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Ajax in ASP.NET" this article.
$.ajax sends a get request to a normal page
This is the simplest way. First, simply understand the syntax of jQuery ajax. The most commonly used method of calling is as follows: $.ajax ({settings}); there are several commonly used setting. All parameters and their explanations can be queried in the official API document of jQuery.
1. Type: request method get/post
2. Url: the requested Uri
3. Async: whether the request is asynchronous
4. Headers: custom header parameter
5. Data: the parameter sent to the server
6. DataType: parameter format, such as string, json, xml, etc.
7. Contents: determines how to parse a "string / regular expression" map of response
8. ContentType: the content encoding type of the amount of data sent to the server, whose default value is "application/x-www-form-urlencoded; charset=UTF-8".
9. Success: the handle to be called after a successful request
10.error: the handle to be called after the request fails
I have never used jQuery's ajax words to look at it like this. Let's take a look at a simple example.
First create a new WebApplication using Visual Studio, introduce jQuery.js into project, and then add two pages, Default.aspx for testing
Default.aspx
The copy code is as follows:
Ajax
Html, body, form
{
Width: 100%
Height: 100%
Padding: 0px
Margin: 0px
}
# container
{
Margin: 100px
Height: 300px
Width: 500px
Background-color: # eee
Border: dached 1px # 0e0
}
...
Then add the JavaScript test code
The copy code is as follows:
Function testPost2 () {
Web.WebService.GetStudent (1, function (result) {
SetContainer (result.ID)
SetContainer (result.Name)
}, function () {
SetContainer ('errology')
});
}
The test code needs to show the full path to write the WebService definition method, the WebService namespace. WebService class name. Method name, and the first few parameters in and out of the parameter list are the parameter list of the calling method. Because GetStudent has only one parameter, only one is written. If there are two parameters, write two in sequence, and the other two parameters can be clearly seen as response success / failure handlers. Take a look at the implementation results:
After careful observation, you will find that there is a benefit to use the combination of ScriptManager and WebService. When the Student object is returned in WebService, it is not serialized into a string, but directly returned. Look at the figure above and find that the object has been automatically converted into a json object, and the result result can be manipulated directly. The response we got in the previous example is a json string, which needs to be converted to a json object with eval on the client side.
ScriptManager+WebSefvice calls ajax with great convenience, but at the same time at the expense of a lot of flexibility, we can't specify a lot of settings like jQuery. Is there a way to have the best of both worlds?
$. Ajax+WebService
It's almost perfect for jQuery to call Handler, but it can't handle multiple methods. In the above example, we can find that WebService can achieve this function, so can jQUery call different methods of WebService? The answer is yes, try jQuery calling the second method just defined by WebService. Write a test function
The copy code is as follows:
Function testPost3 () {
$.ajax ({
Type: 'post'
Url: 'WebService.asmx/GetDateTime'
Async: true
Data: {isLong: true}
Success: function (result) {
SetContainer ($(result). Find ('string'). Text ())
}
Error: function () {
SetContainer ('errology')
}
});
}
The calling mode hasn't changed much, and it's as simple as ever. Just change URL to WebService path + the name of the method you need to call, and then put the parameters in data. Let's look at the results:
As you can see from the figure above, the jQuery call WebService returns an XML document by default, and the data you need is in the node, and you only need to use jQuery to parse xml syntax to get the data easily. What if you want to return a json object? Then you have to use json.net serialization as you would call Handler, and then the front end uses eval transformation, which is not too complicated. I use this pattern most often in projects, which not only maintains the flexibility of jQuery, but also allows you to write multiple methods to call in a Service, without having to walk through the complex page life cycle.
Json.net and sample source code for this article
Json.net is an open source .net platform to deal with json library, can serialize Dictionay nesting and other complex objects, about its simple use will be summarized in time, you can get its source code and official instructions from codeplex.
The above is all the content of the article "how to use Ajax in ASP.NET". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.