How to use Ajax on Visualforce page on Salesforce platform

Just use Ajax pattern to call object data from server on visualforce page.

Following is the Asynchronise demo:

<apex:page >
<script type="text/javascript">
    var __sfdcSessionId = '{!GETSESSIONID()}';
</script>

<script src="../../soap/ajax/29.0/connection.js" type="text/javascript"></script>

<script type="text/javascript">
    window.onload = setupPage;
    function setupPage(){
        var state = {
            output : document.getElementById("output"),
            startTime : new Date().getTime()
        };
        
        var callBack = {
            onSuccess : layoutResults,
            onFailure : queryFailed,
            source : state
        };
        
        sforce.connection.query("Select Id, Name, Industry From Account order by Industry", callBack);
    }
    
    function queryFailed(error, source){
        source.output.innerHTML="An error has occurred: " + error;
    }
    
    function layoutResults(queryResult, source){
        if(queryResult.size > 0){
            var output = "";
            var records = queryResult.getArray('records');
            for(var i = 0; i < records.length; i++){
                var account = records[i];
                output += account.Id + " " + account.Name + " [Industry - " + account.Industry + "]<br>";
            }
            source.output.innerHTML = output;
        }
    }

</script>

<div id="output"></div>

</apex:page>

 

If we want to use the Synchronise model. Just use the query function without callback.  Following is the demo code.

sforce.connection.query("Select Id, Name, Industry From Account order by Industry")

 

If you want to know more about the detail. Please go to click this link:  http://www.salesforce.com/us/developer/docs/ajax/apex_ajax.pdf  

...................................

 

Another link show you another ajax call code-behind function : http://www.cnblogs.com/mingmingruyuedlut/p/3450753.html 

 

你可能感兴趣的:(Ajax)