Salesforce RemoteAction and webService

In Salesforce,we can make asynchronous requests from Visualforce Page,via RemoteAction or webService.

I will share you the usage of RemoteAction and webService In this post.

RemoteAction

Using RemoteAction(JavaScript Remoting) allows us to call methods in Apex controllers and get a callback with return data from JavaScript.
The @RemoteAction methods in Apex must be static and either global or public.
The following is the sample code for RemoteAction.

RemoteActionDemo.page

RemoteActionDemoCls.cls

Response

webService

Using webService(Apex in AJAX) need to include the following lines in your AJAX code
The webService methods in Apex must be static and Class must be global. Here is the sample code for webService.

WebServiceDemo.page

WebServiceDemoCls.cls

Response

The Difference

1. Using webService, can call not only the Page Controllers but also other Classes, which can be Common Classes.
2. Using webService, will cost API request,and RemoteAction will not.
3. Using webService, the methods of Class must be global.