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
katrinaxxkatrinaxx 

Accessing Apex WebService from jQuery

I need to know if it's possible to retrieve some data from SFDC via jQuery.  I created a sample apex webservice to do this.  Then the webservice will be accessed from a 3rd party website via jQuery.  Problem is when it executes the processResponse() function, an exception is thrown because the xmlHttpRequest.responseXML is NULL.  What seems to be the problem?  =(

 

Here's the code of the webservice:  

global class TARetrieveArticlesWebService {
     WebService static String GetArticles()
     {
          In_Depth__c sampleData = [SELECT Name FROM In_Depth__c LIMIT 1];
          return sampleData.Name;
     }
}

 

And here's the code that I placed in the 3rd party site: 

<head>
     <script src='https://xxxxxxxx.full.cs1.force.com/SiteName/resource/jQuery_1_4_2' type='text/javascript'/>
</head>

 

<script type='text/javascript'>
var soapMessage = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><soap:Body><methodCall>GetArticles</methodCall></soap:Body></soap:Envelope>";

 

$.ajax({
     url: "http://xxxxxxxx.full.cs1.force.com/SiteName/services/Soap/class/TARetrieveArticlesWebService/GetArticles/",
     type: "POST",
     dataType: "xml",
     data: soapMessage,
     complete: processResponse,
     contentType: "text/xml; charset=\"utf-8\""
     });

 

function processResponse(xmlHttpRequest, status)
{
     $(xmlHttpRequest.responseXML).find('methodResponse').each(function()
     {
          var name = $(this).find('response').text();
          alert(name);
     });
}
</script>

Henry AkpalaHenry Akpala

Don't you need to pass the sessionId or authentication parameter?

 

Regards

-H