Get started with Ajax

Source: Internet
Author: User

Some time ago, I had been worried that I could not use JS to call the background method. Now I finally learned how to use it. I think I should write it down and share it with others, hoping to help some birds like me.

1. If no parameter method with return value is called in the background, <% = method () %> is undoubtedly the simplest method. For example, if your background method is public string s () {return "";} in this way, you only need to write <script type = "text/javascript"> var a = <% = s () %>; alert ();
</Script>. In this way, the pop-up box displays "I am great ". Of course, you can also skip the box. In short, you can call the method to the background, but the premise is that it must have a return value. Maybe you will say that if I want to assign parameters to the method on the frontend? This method won't work, because the method called at the front end always runs first when loading the server, so it runs without waiting for your parameter to pass. However, if your parameter is just a simple string, that is acceptable. For example: var id = "China "; var a = '<% = s ("' + id + '") %>'; alert (a); the background method is: public string s (string x) {return x + "great";}. The pop-up box displays "great in China ". Note that there is no difference between double quotation marks and single quotation marks in Javascript. In fact, "'+ id +'" is only a string and has no meaning. A gets the return value of the background method. a = "'+ id +'" + "great ". I believe you should understand that the foreground cannot pass variables to the background, but uses the form of a connection string to treat the string as a parameter. In this way, after the background method returns a value, id.
2. I think this method is the best way to call the background. There are also many places on the Internet, that is, aJaxpro. This method is used.
Step 1: Download aJaxpro. dll file, this official website can be, web site http://www.ajaxpro.info /;
Step 2: Add a reference. Add the reference of ajaxpro. dll to your project;
Step 3: add nodes in web. config

<HttpHandlers>
<Add verb = "POST, GET" path = "ajaxpro/*. ashx" type = "AjaxPro. AjaxHandlerFactory, AjaxPro.2"/>
</HttpHandler>
Step 4: Add registration in the Page_Load () event.
AjaxPro. Utility. RegisterTypeForAjax (typeof (Class Name ))And add
[AjaxPro. AjaxMethod], For example
[AjaxPro. AjaxMethod]
Public DateTime GetServerTime ()
{
Return DateTime. Now;
}
Step 5: Finally, you can call the background method in JS. Remember the call method format:
Namespace. Class Name. Method (callback); for example, if the namespace of my class is mYspace, the class is Name, and the method is delete (),
We should call mYspace like this. name. delete (callback ). callback; function callback (res) {alert (res. value);} is the processing result after you call the method, and res is the return value of the method. Do not write callback.
MYspace. Name. delete (). Of course, if your method has parameters, you should add parameters in brackets. There is another method called ajax. dll on the Internet. If you use this method, adding nodes in web. config is a little different.Type = "AjaxPro. AjaxHandlerFactory, AjaxPro.2"Change
Type = "AjaxPro. AjaxHandlerFactory, Ajax". In addition, you do not need to add a space name when calling the class name. Method ().

It took me half an hour to write it. I hope my article can help others, because I often read other people's articles to obtain knowledge, which is similar to the open source, let's take everything we know and learn from each other!


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.