function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Adam BengtsonAdam Bengtson 

Allow visualforce remoting on a basic html page

I have an issue where I created a visualforce page that uses js visualforce remoting.  I have to move this page into a communities subpage which does not allow you to host it as it is, instead you have to drop it in as html but in the process you cannot carry over the apex tags:
 
<apex:page controller="ApexController" sidebar="false" showHeader="false" applyBodyTag="false" applyHtmlTag="false">
Now I no longer have the connection to my controller.  Is there a way I can connect this back in a different way so I can use the visualforce remoting api?
NagendraNagendra (Salesforce Developers) 
Hi Adam,

JavaScript remoting in Visualforce provides support for some methods in Apex controllers to be called via JavaScript.

JavaScript remoting has three parts:
  • The remote method invocation you add to the Visualforce page, written in JavaScript.
  • The remote method definition in your Apex controller class. This method definition is written in Apex, but there are few differences from normal action methods.
  • The response handler callback function you add to or include in your Visualforce page, written in JavaScript.
 To use JavaScript remoting in a Visualforce page, add the request as a JavaScript invocation with the following form:
[namespace.]controller.method( [parameters...,] callbackFunction, [configuration] );
  • Namespace is the namespace of the controller class. This is required if your organization has a namespace defined, or if the class comes from an installed package.
  • Controller is the name of your Apex controller.
  • Method is the name of the Apex method you’re calling.
  • Parameters is the comma-separated list of parameters that your method takes.
  • CallbackFunction is the name of the JavaScript function that will handle the response from the controller. You can also declare an anonymous function inline. callbackFunction receives the status of the method call and the result as parameters.
  • Configuration configures the handling of the remote call and response. Use this to specify whether or not to escape the Apex method’s response. The default value is {escape: true}.
Please find the below sample example on a normal visualforce page:

Visuaforce Page:
<apex:page controller="sample">
    <script type="text/javascript">
    function getAccountJS() 
    {
        var accountNameJS = document.getElementById('accName').value;        
        sample.getAccount( accountNameJS, 
        function(result, event)
        {
            if (event.status) 
            {
                // demonstrates how to get ID for HTML and Visualforce tags
                document.getElementById("{!$Component.theBlock.thePageBlockSection.theFirstItem.accId}").innerHTML = result.Id;
                document.getElementById("{!$Component.theBlock.thePageBlockSection.theSecondItem.accNam}").innerHTML = result.Name;
            } 
            else if (event.type === 'exception') 
            {
                document.getElementById("errors-js").innerHTML = event.message;
            } else 
            {
                document.getElementById("errors-js").innerHTML = event.message;
            }
        }, {escape:true});
    }
    </script>
    Account Name :<input id="accName" type="text" />
    <button onclick="getAccountJS()">Get Account</button>
    <div id="errors-js"> </div>
    <apex:pageBlock id="theBlock">
        <apex:pageBlockSection id="thePageBlockSection" columns="2">
            <apex:pageBlockSectionItem id="theFirstItem">
                <apex:outputText id="accId"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem id="theSecondItem" >
                <apex:outputText id="accNam" />
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Apex Controller:
global class sample 
{
    public String accountName { get; set; }
    public static Account account { get; set; }
    public sample() { }
    
    @RemoteAction
    global static Account getAccount(String accountName) 
    {
        account = [select id, name, phone, type, numberofemployees from Account where name = :accountName ];
        return account;
    }
}

Output:
User-added image
For visualforce remoting on a html page please refer to the below link
https://developer.salesforce.com/docs/atlas.en-us.salesforce1.meta/salesforce1/vf_dev_best_practices_approaches_remoting.htm


Please mark this post as solved if this helps.

Best Regards,
Nagendra.P