Access Salesforce from static HTML using Java Script ( Using Salesforce Username and Password)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script src="https://na7.salesforce.com/soap/ajax/21.0/connection.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsforce/1.8.0/jsforce.js" type="text/javascript"></script>

<script type="text/javascript">

window.onload = function(){

try
{
  //var jsforce = require('jsforce');
var conn = new jsforce.Connection({
  // you can change loginUrl to connect to sandbox or prerelease env.
  loginUrl : 'https://test.salesforce.com'
});
   var username ='xxxxxxxxxxxx';
   var password ='xxxxxxxxx';
conn.login(username, password, function(err, userInfo) {
  if (err) { return console.error(err); }
  // Now you can get the access token and instance URL information.
  // Save them to establish connection next time.
  console.log(conn.accessToken);
  console.log(conn.instanceUrl);
  // logged in user property
  console.log("User ID: " + userInfo.id);
  console.log("Org ID: " + userInfo.organizationId);
  // ...
 
  conn.sobject("Lead").insert({ lastname : 'MyNameCarConfig2',Email:'test@email.com' }, function(err, lead) {
  if (err || !lead.success) { return console.error(err, lead); }
  console.log("Created record id : " + lead.id);
  // ...
});

});

 } catch(error)
{
 alert('Catch'+error);
}
}

</script>
</head>
<body>

</body>
</html>

Errors:

Err:Failed to load https://test.salesforce.com/services/Soap/u/39.0: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

Sol: I have installed CORS extension in chrome browser to avoid this error.


Comments