1

I have an Apex class implementing a simple REST GET method

@RestResource(urlMapping='/myrest/*') 
global class MyResService {

   @HttpGet
   global static CustomObject__c doGet() {
     ..
   }
}

and a Visualforce page including Javascript code that requests this service's (JSON result)

<apex:includeScript value="{!$Resource.jquery_min_js}" />
<script type="text/javascript">
   $j = jQuery.noConflict();
   $j.ajax({
    url: '{!InstanceUrl}/services/apexrest/myrest',
    type: 'GET',
    dataType: 'json',
    error: function() { console.log(error); },
    success: function() { alert('hello!'); },
    beforeSend: setHeader
   });

   function setHeader(xhr) {
       xhr.setRequestHeader("Authorization", "OAuth {!OAuthToken}");
   }

 ...

My question now is how to I get the {!OAuthToken} and {!InstanceUrl}? I have seen quite complex solutions that seem to be required when calling ANOTHER ORG's service. I need the simple ONE ORG solution ;).

  • 2
    Why not simple static Apex method exposed as webservice and accessed with AJAX toolkit? Or if it's on VF page - read about @RemoteAction? – eyescream Mar 20 '13 at 9:20
  • Yes I am also wondering why JavaScript Remoting is not a better solution for you here? Not that, unlike REST API, it will not count against the orgs 24hr rolling API limit. – Andrew Fawcett Mar 20 '13 at 12:09
  • See above comments, are {!$Api.Enterprise_Server_URL_270} and {!$Api.Session_ID} any good? – bigassforce Mar 20 '13 at 12:18
6

The solution provided by Ivo Rocha is nice but too complex for my case. When I thought deeper about my problem I found out that Apex REST services are not the best solution when you just need to pass their result to a VF page in the same org.

The solution is: Use Javascript Remoting instead!

3

I've already implemented a solution that looked like this. I've used the forceTK javascript plugin. Download it and use it as a static resource.

Then create a function like this:

<script>
    var client = new forcetk.Client();
    client.yourAjaxNewFunction = function(your_params){
        var url = this.instanceUrl + '/services/apexrest/yourRest';
        $j.ajax({
            async: this.asyncAjax,
            url: url,
            type: 'GET',
            dataType: 'json',
            contentType: 'application/json',
            error: function() { console.log(error); },
            success: function() { alert('hello!'); },
            beforeSend: function (xhr) {
                if (this.proxyUrl !== null) {
                     xhr.setRequestHeader('SalesforceProxy-Endpoint', url);
                }
                xhr.setRequestHeader(this.authzHeader, "OAuth " + this.sessionId);
                xhr.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion);
                xhr.setRequestHeader('param1', param1Value);
                xhr.setRequestHeader('param2', param2Value);
            }
        });
    };
</script>

To use it just call the new "yourAjaxNewFunction" client method

<script>
  client.yourAjaxNewFunction(your_params);
</script>  

Hope it helps ;)

  • I get a "parsererror" when I make a call using the above method. I confirmed that there is nothing wrong with the service by testing it from workbench.developerforce.com. It only errs out when I access it via an ajax call. Any ideas? – DCBoy Mar 22 '13 at 19:03

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.