Quickguide for Ajax [Quick Guide to simplified Ajax] and extensions to existing WebServices.

Source: Internet
Author: User

Ajax. Net wrapper quick Usage Guide
Karl Seguin-http://www.openmymind.net/-copyright 2005

(View ajaxguide.doc for more detailed information)

Step 1-
Create a reference to the Ajax. dll file

Step 2-set up the httphandler
In the Web. config, set up the httphandler, like:

<Configuration>
<System. Web>
<Httphandlers>
<Add verb = "post, get" Path = "ajax/*. ashx" type = "Ajax. pagehandlerfactory, Ajax"/>
</Httphandlers>
...
<System. Web>
</Configuration>

Step 3-
In the page_load event, call the following function:

'Vb. net
Public class index
Inherits system. Web. UI. Page

Private sub page_load (sender as object, e as eventargs) handles mybase. Load
Ajax. Utility. registertypeforajax (GetType (INDEX ))
'...
End sub
'...
End Class

// C #
Public class index: system. Web. UI. Page {
Private void page_load (Object sender, eventargs e ){
Ajax. Utility. registertypeforajax (typeof (INDEX ));
//...
}
//...
}

 

Step 4-
In the codebehind of your page, add functions (like you normally wowould) that you 'd like to be able to asynchrounsly called by client-side scripting. mark these functions with the Ajax. javascriptmethodattribute ():

// C #
[Ajax. ajaxmethod ()]
Public int serversideadd (INT firstnumber, int secondnumber)
{
Return firstnumber + secondnumber;
}

'Vb. net
<Ajax. ajaxmethod ()> _
Public Function serversideadd (byval firstnumber as integer, byval secondnumber as integer) as integer
Return firstnumber + secondnumber
End Function

The wrapper will automatically create a JavaScript function named "serversideadd" which accepts to parameters. When called, this server-side function will be called and the result returned.

Step 5-
Using JavaScript, You Can invote the serversideadd function like you wowould any other JavaScript function. you can call it using the two parameters, or optionally pass a call back function. the name of the function is, by default, <name of class>. <name of server side function>, such as index. serversideadd:

Alertindex. serversideadd (100,99 ));

Or

Index. serversideadd (100,99, serversideadd_callback );

Function serversideadd_callback (response ){
Alert (response. value );
}

The response exposes three properties, error, value and request.

Note that you can return more complex objects that simple types.

See the demo for additional information:
Http://ajax.schwarz-interactive.de/csharpsample/default.aspx

========================================================== ============================

This is a description file after downloading Ajax. Simple and rough translation (not translated according to the original article ):
Add an Ajax method as follows:
Step 1: Add a reference to Ajax. dll in the project.
Step 2: Modify the Web. config file: Add a reference to handle.
<Configuration>
<System. Web>
<Httphandlers>
<Add verb = "post, get" Path = "ajax/*. ashx" type = "Ajax. pagehandlerfactory, Ajax"/>
</Httphandlers>
...
<System. Web>
</Configuration>
Step 3: Add the following function when loading the page:
// C #
Public class index: system. Web. UI. Page {
Private void page_load (Object sender, eventargs e ){
Ajax. Utility. registertypeforajax (typeof (INDEX ));
//...
}
//...
}
Note: It seems that the Web Service is "misinterpreted! Especially the following functions.
Step 4: add the Ajax function on the page to make it have the attributes: Ajax. javascriptmethodattribute ():
// C #
[Ajax. ajaxmethod ()]
Public int serversideadd (INT firstnumber, int secondnumber)
{
Return firstnumber + secondnumber;
}
Note: The function here is added in the background page, rather than an independent WebService! Essentially, WebService is based on the HTTP protocol like normal pages. Therefore, there is no essential difference between normal pages and asmx pages of WebService. Program When referencing, some protocols are different. However, I personally think that AJAX is not very desirable. It puts service functions and page functions together, and there will be some problems in management. In addition, the service function may be called by other programs, but it can only be called by JS on its own page.
Step 5: JS call:

Alert (index. serversideadd (). Value); // the original text is incorrect. The original text is: alertindex. serversideadd ));

Or

Index. serversideadd (100,99, serversideadd_callback );

Function serversideadd_callback (response ){
Alert (response. value );
}

OK!

However, I do not understand the following issues:
1. Ajax functions are on the page. Will this cause confusion in the page logic? I don't know which is a service function or a page function.
2. js calls are only on this page. What if we want to call a function or service across pages? (Later I learned that class references can be called across pages .)
3. Can I directly call an existing service function? If a large number of service functions already exist in the project, can I change them to page functions one by one? I think it is not advisable to add the Ajax attribute! The most depressing thing is that AJAX registration during page transfer is unacceptable. Think about it. In that case, how much content do you want to change?

Therefore, I think we are faced with the problem of using js to directly call functions on WebService! Instead of modifying the page function like Ajax and calling it with JS! Ajax transformation of existing WebService.

1. Register Ajax in a user control or page:
Ajax. Utility. registertypeforajax (typeof (Webb. Wave. WebService. service4folderandfiles ));
You can register any class, not limited to the page class. Of course, your function can also be written in web service.
2. modify the original service function and add Ajax attributes:
[Ajax. ajaxmethod ()]
[Webmethod]
Public int serversideadd (INT firstnumber, int secondnumber)
{
Return firstnumber + secondnumber;
}
That is, you can give a function two attributes, so that yourCodeThere is no need to modify too much.
3. js calling is no different from the above.

Conclusion: As shown in the preceding example, Ajax is no longer limited to service function calls. It foreground functions of the page can be conveniently called in Js. Of course, the original web service functions can not be changed, which provides great convenience for our project transformation. If you are willing to change it, add an Ajax method property. No, you can try again. Of course, the main thing is that the original function can ensure that your original project can run well.

At last, when registering the Ajax class, Ajax. Utility. registertypeforajax (typeof (Webb. Wave. WebService. service4folderandfiles ));
It adds some JS Code to the page. These codes request the functions on the server, and the server uses handle to process different requests to locate the functions on the server.

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.