1

I am wondering if I can use the standard $.ajax, $.post or $.get with visual force. If so what do I put in the url? Can I get the url of the method I want to make a call to?

4
2

Check this post Rest API (POST method) with Jquery

GET, POST AND DELETE Method using Jquery

You can use stndard services or You can create own rest service and use in jquery

If you want callout to other org then in header you need to pass userName, password and security token

<apex:page>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <div>
        <input  class="btn" value="Get Account list" type="button" onclick="getSobjectList()" />
        <input  class="btn" value="Create Account Record" type="button" onclick="createSobjectRecord()" />
        <input class="btn" value="Delete Account Record" type="button" onclick="deleteSobjectRecord()" />
    </div>
    <div id="warehouseList">
    </div>
    <script type="text/javascript">
        var $j = jQuery.noConflict();
        var sessionId = '{!$Api.Session_ID}';
        function getSobjectList() {
            $j.ajax({
                type: "GET",
                url: "/services/data/v35.0/query?q="+''+"{!URLENCODE('SELECT Id, Name FROM Account')}",
                headers: {"Authorization" : "OAuth " + sessionId},
                crossDomain : true,
                dataType: 'application/json',
                success: function (responseData) {
                    console.log(responseData);
                },
                error: function (request, status, error) {
                    console.log(request.responseText);
                }
            });
        }
        var recordId;
        function createSobjectRecord() {
             var accountInfo = { "Name" :"Testing Jquery with Rest"};
             var accountInfoJson = JSON.stringify(accountInfo);
             $j.ajax({
                type: "POST",
                url: "/services/data/v35.0/sobjects/Account",
                headers : {
                        'Authorization' : "OAuth " + sessionId,
                        'Content-Type' : 'application/json'
                  },
                crossDomain : true,
                data: JSON.stringify(accountInfo),
                dataType: 'json',
                success: function (responseData, status, xhr) {
                    console.log(responseData.id);
                    recordId = responseData.id;
                },
                error: function (request, status, error) {
                    console.log(request.responseText);
                }
            });
        }
        function deleteSobjectRecord(){
            $j.ajax({
                type: "Delete",
                url: "/services/data/v35.0/sobjects/Account/"+recordId,
                headers : {
                        'Authorization' : "OAuth " + sessionId,
                        'Content-Type' : 'application/json'
                  },
                crossDomain : true,
                success: function (responseData, status, xhr) {
                    console.log(responseData);
                },
                error: function (request, status, error) {
                    console.log(request.responseText);
                }
            });
        }
    </script>
</apex:page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.