Javascript 角度-如何使用$http使用HTTPS

Javascript 角度-如何使用$http使用HTTPS,javascript,ajax,angularjs,angular-http,Javascript,Ajax,Angularjs,Angular Http,只是想问一下如何使用$http处理https+跨域url 我已经使用了$.ajax来解决我的问题,但是我之所以想更多地使用$http是因为我有一个$http拦截器,因为使用$.ajax需要更多的代码行,而不是使用拦截器来自动化一些过程 我读过类似的帖子[,],但它们都没有解决我的问题 我有一个附加的控制台屏幕截图,我希望它有意义: 奇怪的是,$http使用的选项方法,我将其设置为POST 以下是我的代码快照: $http({ method: 'POST', url: '/Hea

只是想问一下如何使用
$http
处理
https
+
跨域
url

我已经使用了$.ajax来解决我的问题,但是我之所以想更多地使用$http是因为我有一个$http拦截器,因为使用$.ajax需要更多的代码行,而不是使用拦截器来自动化一些过程

我读过类似的帖子[,],但它们都没有解决我的问题

我有一个附加的控制台屏幕截图,我希望它有意义:

奇怪的是,$http使用的选项方法,我将其设置为POST

以下是我的代码快照:

$http({
    method: 'POST',
    url: '/HearingCentre',
    data: {
        Type: ['P', 'V']
    },
    success: function (res) {
        console.log(res);
        d.resolve(res);
    },
    error: function (res) {
        console.log(res);
        d.reject(res);
    }
 });
注意:我在这里使用$http拦截器,因此基本上URL将附加基本API URL

$.ajax版本

$.ajax({
    method: 'POST',
    crossDomain: true,
    url: 'https://ahtstest.hearing.com.au/services/api/HearingCentre',
    data: {
        Type: ['P', 'V']
    },
    success: function (res) {
        console.log(res);
    },
    error: function (res) {
        console.log(res);
    }
});
有人能解决这个问题吗

var userApp = angular.module('userApp', []);
    userApp.controller('UserController', function ($scope,$http) {

        //server call with angular 
        $scope.makeServerCall = function() {
                var url = '/root/jsp/catalog/xml/getJsp.jsp?PatientId=9175;
                $http({
                    crossDomain: true,
                    xhrFields: {withCredentials: false},
                    headers: {'Accept': 'application/json','Content-Type': 'application/json'},
                    method: 'GET',
                    url: url
                }).then(function(response) {
                    console.log("Success: "+JSON.stringify(response));
                },function(response) {
                    console.log("Error:   "+JSON.stringify(response));
                });
        };
  }
并确保在服务器端可以访问跨域reuqest。假设我使用的是SpringMVC,所以我添加了一个过滤器,并配置跨域请求以允许如下操作。如果您使用任何其他语言,比如PHP,那么您必须设置响应头。您可以搜索相同的响应头

我的服务器过滤器SpringMVC

@Component
public class EcwCORSFilter implements Filter {

        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
                HttpServletResponse response = (HttpServletResponse) res;
                response.setHeader("Access-Control-Allow-Origin", "*");
                response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
                response.setHeader("Access-Control-Max-Age", "3600");
                response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With,isAjaxRequest");
                chain.doFilter(req, res);
        }

        public void init(FilterConfig filterConfig) {
        }

        public void destroy() {
        }

}

这是Chrome预处理您的请求,以便查看您的服务器允许哪些方法。您需要在服务器上允许CORS。在google chrome中安装CORS插件,然后启用它,这可能会有所帮助…@limelights我明白了…,对不起,我没有访问后端的权限,但我会让他们知道允许在服务器上使用CORS。@Ahmer插件不起作用。谢谢你。