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
REST API and JavaScritpsREST API and JavaScritps 

How to connect a SalesForce using javascritp but without page login.sales.com

How could I connect to salesforce in javascritp but without using page login.salesforce.com.? My purpose is then to create a query to the "Lead" entity and incorporated it as a gadget in a business mashup platform test Since I have the customer key and secret key of SalesForceDeveloper. Thanks.
SuperfellSuperfell

The client id & secret identity the client application, not any specific user, so you'll still need to go through some sort of authentication flow to get a sessionId (Oauth, merge fields from the web ui etc)

REST API and JavaScritpsREST API and JavaScritps

Hi.

 

Then, How to connect a SalesForce but using javascript?

 

I have been test but still does not work.

 

Can you help me?

 

 

SuperfellSuperfell

Here's a quick VF page that demonstrates.

 

 

<apex:page>

  <h1>Recent Items via REST APi</h1>
  
  <div id='mytable'></div>
  
<script type="text/javascript">
function makeRestRequest() {
    http = new XMLHttpRequest();
    http.open("GET", "/services/data/v20.0/recent.json", true);
    http.setRequestHeader("Authorization", "OAuth {!$Api.Session_ID}");
    http.onreadystatechange=function() {
      if (http.readyState==4) {
       j = JSON.parse(http.responseText);
       mytable = document. getElementById('mytable');
       for (r in j)
           mytable.innerHTML += j[r].Name + "<BR>";
      }
    }
    http.send(null);
}

var previousOnload = window.onload;        
window.onload = function() { 
   if (previousOnload) { 
       previousOnload();
    }
    makeRestRequest();
}
</script>
</apex:page>

 

 

 

 

REST API and JavaScritpsREST API and JavaScritps

Hi.

 

I following the code, but give an error at  j = JSON.parse(http.responseText);

 

The property http.responseText is in blank, string empty. Somebody tell what I am doing wrong?

 

Is there a site where I can find information about using javascript with Rest API in SalesForce?

 

 

 

 

SuperfellSuperfell

Other than the Authorization header, there's nothing specific going on, its just like all the sites that expose data over json. (FWIW, the code i posted ran fine for me in Chrome). Is the REST APi enabled for your login ?

jon-wujon-wu

This was useful to me but I noticed this code doesn't actual work in my VF pages directly because of cross domain restrictions. I had to use the AJAX Toolkit to proxy the REST request with the OAuth token to get it to work because the VF page is served from a different domain from the API. Because the API redirects to another domain, there is no response and an error is thrown.

 

Is there any way around this with the current REST API? It's not a showstopper but it's inconvenient and at once you're proxying through it seems like I'd just use the AJAX toolkit anyways.

AshiAshi

Hi,

 

Could you find solution for this. My requirement is very much similar, where I want to authenticate my application without visiting Salesforce login page and I need to do this using Jquery $.ajax object.

 

Thanks