1

I'm trying to get a simple example to work with forcetk.js, where I call an apex rest service defined in the same Sandbox as the vf page below. I've tried different flavors of the URL, but the error event is always called. I've created remote sites for different versions of the Sandbox' host name, but that hasn't resolved the issue. When I specify a URL without the domain (a relative URL), I get a 302 error response. When I use the Location that Salesforce returns for the 302 response, I get a 401 cross-domain script error. I'm very new to forcetk.js, so its likely I'm doing something simple wrong. Would someone please take a look at my vf page below and tell me what am I doing wrong?

<apex:page sidebar="false">

<apex:includeScript value="{!URLFOR($Resource.jqueryui_1_11_4_custom,'/jquery-ui-1.11.4.custom/external/jquery/jquery.js')}"/>
<apex:includeScript value="{!$Resource.forcetk}"/>

<h1>ForceTK Test Page</h1>

<script type="text/javascript">
var j$ = jQuery.noConflict();
</script>

<script type="text/javascript">
    var client = new forcetk.Client('{!$Api.Session_ID}');
    client.yourAjaxNewFunction = function(mediaPlanId){
        //https://somecompany--env--c.cs7.visual.force.com/services/apexrest/mediaPlanDetail?mediaPlanId=2271&pageSize=10&_search=false&nd=1437750768584&rows=10&page=1&sidx=&sord=asc
        //var url = this.instanceUrl + '/services/apexrest/mediaPlanDetail?mediaPlanId=2271&pageSize=10&_search=false&nd=1437750768584&rows=10&page=1&sidx=&sord=asc';
        var url = '/services/apexrest/mediaPlanDetail?mediaPlanId=2271&pageSize=10&_search=false&nd=1437750768584&rows=10&page=1&sidx=&sord=asc';
        j$.ajax({
            async: this.asyncAjax,
            url: url,
            type: 'GET',
            dataType: 'json',
            contentType: 'application/json',
            error: function() { 
                //console.log(error);
                alert ( '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("Authorization", 'Bearer {!$Api.Session_ID}');

                //xhr.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion);
                xhr.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + '22');

            }
        });
    };
</script>

<form action="/">
<input type="button" onclick="client.yourAjaxNewFunction('2271');" />
</form>

</apex:page>

UPDATE

Following is the latest version of my VP page. When I click the button, my browser is reporting an error:

Uncaught TypeError: Cannot read property 'ajax' of undefined

The JavaScript statement reporting this error is in the forcetk.js code

<apex:page sidebar="false">

<apex:includeScript value="{!URLFOR($Resource.jqueryui_1_11_4_custom,'/jquery-ui-1.11.4.custom/external/jquery/jquery.js')}"/>
<apex:includeScript value="{!$Resource.forcetk}"/>

<h1>ForceTK Test Page</h1>

<script type="text/javascript">
var j$ = jQuery.noConflict();
</script>

<script type="text/javascript">
    var forceTKClient = new forcetk.Client();
    forceTKClient.setSessionToken('{!$Api.Session_ID}');

function callApex(mediaPlanId)
{
    var url = '/mediaPlanDetail' + 
        '?mediaPlanId=' + mediaPlanId + 
        '&pageSize=10' +
        '&rows=10' + 
        '&page=1' + 
        '&sidx=&' + 
        'sord=asc' +
        ''
        ;

        forceTKClient.apexrest(
            url,
            function(data, textStatus, jqXHR) {
                console.log('SUCCESS - ' + data);
            },
            function(jqXHR, textStatus, errorThrown) {
                console.log('ERROR - ' + textStatus);
            },
            'GET',
            '',
            null,
            false
         );
    };

</script>

<form action="/">
<input type="button" onclick="callApex('2271');" />
</form>

</apex:page>
10
  • 401 indicates you have are not authorized.
    – Adrian Larson
    Jul 24, 2015 at 17:03
  • I thought the idea of using forcetk.js is that I wouldn't need to authenticate with the other sf domain. I am passing in a session id when I create the forcetk client. Why doesn't that work? Jul 24, 2015 at 17:08
  • 1
    You might want to look at this question to get some further details: developer.salesforce.com/forums/?id=906F00000009AtcIAE Jul 24, 2015 at 20:35
  • Your not actually setting the session token correctly. Jul 24, 2015 at 20:36
  • 1
    Actually, try removing your var j$ = jQuery.noConflict();. You are no longer using that line and I think it's causing conflicts with the internal forcetk.js library. Jul 27, 2015 at 15:53

2 Answers 2

2

By reviewing the the following question and answer you will find the correct syntax needed to make the appropriate forcetk.js call: https://developer.salesforce.com/forums/?id=906F00000009AtcIAE

Once that is done, please ensure that you update your jquery noconflict variable to something other than j$ to ensure that you do not have conflicts with the forcetk.js library.

0

According to the github documentation, access /services/data doesn't require a proxy server, but /services/apexrest does require a proxy.

Your code shows an apexrest endpoint. Have you set-up a proxy and overridden the cross-domain issue?

I'll download the example from https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit to get that working first before trying yours.

1
  • Can you be more specific by what you're referring with "have you set-up a proxy and overridden the cross-domain issue" I thought the point of forcetk.js was to get around the cross-domain issue between VF pages and Apex web services? Jul 27, 2015 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .