Newer Version Available
Namespaces and JavaScript Remoting
You can use the $RemoteAction global to
automatically resolve the correct namespace, if any, for your remote action. This makes it
easier to work with namespaces, especially for pages that make remoting calls to methods
provided in packages.
To use this facility, you must explicitly invoke JavaScript remoting.
The pattern for doing this
is:
1Visualforce.remoting.Manager.invokeAction(
2 'fully_qualified_remote_action',
3 invocation_parameters
4);The fully qualified remote action is a string that represents the complete path to the remote action method, including namespace, base class, and so on: namespace[.BaseClass][.ContainingClass].ConcreteClass.Method. Use $RemoteAction in an expression to automatically resolve the namespace, for example {!$RemoteAction.MyController.getAccount}.
Invocation parameters are the arguments used to perform the
remote method invocation, and are the same arguments used to make a standard remoting call:
- The parameters to send to the @RemoteAction method, if any.
- The callback function, which handles the returned result.
- Configuration details for the invocation, if any.
For example, you might define a remote invocation to retrieve an
account like
this:
This
JavaScript remoting call doesn’t need to know the details of the namespace in which the
controller is defined, whether it’s in your own namespace or something provided by an
installed package. It also handles the situation where your organization doesn’t have a
namespace defined.
1<script type="text/javascript">
2function getRemoteAccount() {
3 var accountName = document.getElementById('acctSearch').value;
4
5 Visualforce.remoting.Manager.invokeAction(
6 '{!$RemoteAction.MyController.getAccount}',
7 accountName,
8 function(result, event){
9 if (event.status) {
10 document.getElementById('acctId').innerHTML = result.Id
11 document.getElementById('acctName').innerHTML = result.Name;
12 } else if (event.type === 'exception') {
13 document.getElementById("responseErrors").innerHTML = event.message;
14 } else {
15 document.getElementById("responseErrors").innerHTML = event.message;
16 }
17 },
18 {escape: true}
19 );
20}
21</script>