3
votes

I created the sample REST api class (found here) in my developer org, and I'm trying to hit it from javascript on a visualforce page in the same org, but I'm getting the following error:

"NetworkError: 405 Method Not Allowed - https://na9.salesforce.com/services/apexrest/Account/001E000000B9GjD"

From the guide in the link mentioned above, a 405 means "The request method does not have a corresponding Apex method." Any ideas? Here's the related code:

Apex class:

@RestResource(urlMapping='/Account/*')
global with sharing class MyRestResource {
   @HttpGet
   global static Account doGet(RestRequest req, RestResponse res) {
      ...
   }
}

Visualforce page (in javascript):

var session = '{!$Api.Session_ID}';
var Url = "https://na9.salesforce.com/services/apexrest/Account/001E000000B9GjD";

xmlHttp = new XMLHttpRequest(); 
xmlHttp.onreadystatechange = ProcessRequest;
xmlHttp.open( "GET", Url, true );
xmlHttp.setRequestHeader('Set-Cookie', session);
xmlHttp.send( null );
1
Have you tried playing around with the urlMapping attribute? - Brian Driscoll
I've tried creating another class with a different mapping, and haven't been able to hit that either. - CDelaney
Try testing your service with apigee (www.apigee.com) which will help you discover if the rest service is working properly. I also think you are looking for Javascript rmeoting more than REST Api i this case (see teachmesalesforce.wordpress.com/2011/06/19/javascript-remoting) - pbattisson
apigee.com helped in debugging easily .. thanks a lot pbattisson! - Chirag Mehta
If you're trying to use AJAX to call the REST API, which is on domain "na12.salesforce.com", from a Visualforce page, which is on domain "c.na12.visual.force.com", you're being blocked by JavaScipt's Same-Origin policy, according to which you're performing a Cross-Site Scripting violation. If you can't take pbattison's advice and use JS REmoting, take a look at the Force.com JavaScript REST Toolkit link], which gets around this issue through proxies. - zachelrath

1 Answers

0
votes

My guess is your developer organization is namespaced, meaning you have a managed package. To use an apex REST service in such an organization, you must include your namespace prefix in the URL. So, in this case, you'd send your request to

/services/apexrest/<namespace>/Account/001E000000B9GjD

Hope that helps.