I've come across a situation where the mere presence of an invokeAction
call inside my JavaScript code causes VF to display a 'no remoted actions found to resolve' page.
In short, I have a button that reveals an outputPanel
, and this contains a second button that invokes a remote method via JS (which is also contained therein). The problem is, I only need to click the 'reveal' button and the error page is shown. I suspect this is due in part to VF over-zealously parsing my code, because it happens even if the invokeAction
code is present but commented out.
However, if I relocate the whole <script>
section outside my <form>
then everything works perfectly, and I'm wondering why this might be. FWIW, it makes no difference whether I declare the controller & remote method as global or public.
On a related note, one way of getting around this is to simply invoke the remote method directly:
MyController.doRemoteThing(arg1, arg2, f(r,e){});
Is there any reason not to do it this way in general?
PAGE
<apex:page controller="MyController">
<apex:form>
<apex:commandButton action="{!ButtonPressed}" value="{!IF(wasPressed, 'Hide', 'Reveal')}" reRender="idPanel,idCmd" id="idCmd" />
<apex:outputpanel id="idPanel">
<apex:outputPanel rendered="{!wasPressed}">
<!-- move this outside the <form> and everybody is happy! -->
<script>
function makeRemoteCall() {
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.MyController.doRemoteThing}', 'abcd', 'efgh',
function (result, event) { alert(result); });
}
</script>
<button onclick="makeRemoteCall();return false;">Do Remote Thing</button>
</apex:outputPanel>
</apex:outputPanel>
</apex:form>
</apex:page>
CONTROLLER
public class MyController {
public MyController() { wasPressed = false; }
public Boolean wasPressed {get; set;}
public void ButtonPressed() { wasPressed = !wasPressed; }
@RemoteAction public static String doRemoteThing(String str1, String str2) {
return str1 + str2;
}
}