What is the correct architectural pattern for cross domain REST call in AngularJS?

我的梦境 提交于 2019-12-07 06:40:10

问题


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) {

              }
          });

回答1:


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



来源:https://stackoverflow.com/questions/18541976/what-is-the-correct-architectural-pattern-for-cross-domain-rest-call-in-angularj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!