+ Start a Discussion
sfdc007sfdc007 

REST API Via HTML Page help needed

Hi,

I need help on the following requirement as follows

I have a  External HTML Page(not in visualforce) where i am trying to invoke a REST API call which i have in my developer account on a button click of html page

But when i click on the buttin nothing is happening , let me know what i am doing wrong here

 
MY HTML PAGE :


<html>
   <head> 
     <script type="text/javascript" src="jquery-1.6.4.js"></script>

     <script>
      function getTasks() {
        $.get("https://ap2.salesforce.com/services/apexrest/opportunity",
          function(data){
            alert("Data Loaded: " + data);
          });
    }
    </script>
   </head>
   <body>
      <button onclick="javascript:getTasks();">Show Match</button>
   </body>
</html>


I have inclued the REST API url in Remote site settings call back url
 
REST CLASS IN MY DEVELOPER EDITION:

@RestResource(urlMapping='/opportunity/*')
global with sharing class MyRestResource {

@HttpGet
    global static opportunity doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        //String opportunityId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        opportunity result = [SELECT Id, Name,Account.name,owner.name FROM opportunity WHERE Id = '00628000004KwQ0'];
        return result;
    }

@HttpPost
global static String doPost(String name,
    String phone, String website) {
    opportunity opportunity = new opportunity();
    opportunity.Name = name;
    insert opportunity;
    return opportunity.Id;
}
}

Kindly help me pls

Thanks in Advance

 
KaranrajKaranraj
You must be authenticated into salesforce system before you make call to your custom apex rest class. Here is the link for the Authentication process  - https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_authentication.htm