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

Is it possible to use the salesforce AJAX with visualforce pages deployed on Sites?



I created a VF page with Ajax. It can get fields value from SF with javascript.


All that works well in suck link: But after I put this VF page on sites, it can not get any data.


My VF page ajax code is as below:


try { sforce.connection.login("", "xxxxpasswordxxxxsecurityToken");

alert("Login Success");

var result = sforce.connection.query("SELECT c.Id, c.Name, c.Email, c.Account.Name FROM Contact c");

  xxxxxxx some logic.

} catch(e) { alert(e); }

 It threw some error message:{faultcode:'UNKNOWN_EXCEPTION',faultstring:'UNKNOWN_EXCEPTION:Site under construction',}

In VF page, it can works well, but in site this line threw error

sforce.connection.login("", "xxxxpasswordxxxxsecurityToken");



Who can help me?




Best Answer chosen by Admin (Salesforce Developers) 
Instead of using the API through AJAX to execute that query, why not use Apex controller associated to your VF page? That will also avoid you having to hardcode the username/password into your javascript which is probably not very secure.