AngularJS 中跨域 REST 调用的正确架构模式是什么? [英] What is the correct architectural pattern for cross domain REST call in AngularJS?
问题描述
我创建了一个 AngularJS 服务,该服务直接从客户端访问 Salesforce REST API.但是,由于同源限制,我无法使其正常工作.即使在访问未经身份验证的 REST 服务并同时尝试 $http、$http.json 和 Ajax 时也是如此.我也尝试了很多 Json、Jsonp 等组合.
鉴于我遇到了很多问题,我认为我的一般方法是不正确的.也许我需要为此设置代理服务器?我的后端是 Firebase,所以我目前没有自己的服务器.
我不相信 Salesforce API 支持 COR,我无法改变这一点.
这是我尝试使用 $http 和 Ajax 的示例.
return $http.jsonp('https://na1.salesforce.com/services/data/',{标题:{'内容类型':'应用程序/json','接受':'应用程序/json'}}).成功(功能(数据,状态,标题,配置){回调(数据);控制台调试(数据.json);}).错误(功能(数据,状态,标题,配置){console.debug("getVersions: 检索数据失败:"+eval(data));});$.ajax({url: 'https://na15.salesforce.com/services/data',类型:获取",数据类型:jsonp",发送前:函数(xhrObj){xhrObj.setRequestHeader("Content-Type","application/json");xhrObj.setRequestHeader("Accept","application/json");xhrObj.setRequestHeader("X-Requested-With", "XMLHttpRequest");},成功:功能(数据){控制台调试(数据);回调(数据);},错误:函数(数据){}});
您需要在 SalesForce 中进入远程设置,然后进入 CORS 并将域名列入白名单...
我遇到了同样的问题,它似乎已经解决了问题.
看看这是否有帮助此处>
I've created an AngularJS service that is hitting the Salesforce REST API directly from the client. However, I haven't been able to get it working due to same origin restrictions. Even when accessing non authenticated REST services and trying both $http, $http.json and Ajax. I've also tried lots of combinations of Json, Jsonp etc.
Given that I've had so many issues I'm thinking that my general approach is incorrect. Perhaps I need to setup a proxy server for this? My backend is Firebase so I don't currently have my own server.
I don't believe that the Salesforce API supports CORs and I cannot change that.
Here is an example of what I tried using $http and Ajax.
return $http.jsonp('https://na1.salesforce.com/services/data/',{
headers: {
'Content-type': 'application/json', 'Accept': 'application/json'
}}).
success(function (data, status, headers, config) {
callback(data);
console.debug(data.json);
}).
error(function (data, status, headers, config) {
console.debug("getVersions: failed to retrieve data: "+eval(data));
});
$.ajax({
url: 'https://na15.salesforce.com/services/data',
type: "GET",
dataType: "jsonp",
beforeSend: function(xhrObj){
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Accept","application/json");
xhrObj.setRequestHeader("X-Requested-With", "XMLHttpRequest");
},
success: function (data) {
console.debug(data);
callback(data);
},
error: function(data) {
}
});
You need to go into Remote Settings within SalesForce then CORS and whitelist the domain name...
I had the same issue and it seemed to have resolved the issue.
See if this helps here
这篇关于AngularJS 中跨域 REST 调用的正确架构模式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!