无法从 javascript 访问 apex REST Web 服务
我在我的开发人员组织中创建了示例 REST api 类(在此处找到),并且我尝试从同一组织中的 Visualforce 页面上的 javascript 访问它,但出现以下错误:
“NetworkError: 405 Method Not允许 - https://na9.salesforce.com/services/apexrest/Account/001E000000B9GjD"
从上述链接中的指南来看,405 表示“请求方法没有相应的 Apex方法。” 有什么想法吗?这是相关代码:
Apex 类:
@RestResource(urlMapping='/Account/*')
global with sharing class MyRestResource {
@HttpGet
global static Account doGet(RestRequest req, RestResponse res) {
...
}
}
Visualforce 页面(在 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 );
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 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是您的开发人员组织是命名空间的,这意味着您有一个托管包。要在此类组织中使用 apex REST 服务,您必须在 URL 中包含您的命名空间前缀。因此,在这种情况下,您可以将请求发送给
希望有所帮助。
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
Hope that helps.