2

just wanted to ask on how to deal with https + cross-domain url using $http?

I've used $.ajax to solve my problem, but the reason why I wanted to use $http more is because I have an $http interceptor since using $.ajax takes a lot more line of codes rather than having an interceptor to automate some process.

I've read similar posts [link1, link2, link3] but neither of them have solved my problem.

I have an attached screenshot of my console, I hope it makes sense: http-ajax.

The weird thing is the OPTION Method being used by $http which I have set it to POST

Here's my code snapshot:

$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);
    }
 });

NOTE: I'm using an $http interceptor here so basically the URL will be appended with the base API URL

$.ajax version

$.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);
    }
});

Do anyone have a solution for this?

Community
  • 1
  • 1
Rene Padillo
  • 2,250
  • 4
  • 26
  • 39

1 Answers1

1
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));
                });
        };
  }

And make sure that at your server side you can accessible to cross domain reuqest. Lets say I am using Spring MVC so I have added one filter and configure cross domain request to allow as follow. If you using any other language lets say PHP, then you have to set response header.you can search for the same.

My Server filter Spring mvc

@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() {
        }

}
Mahesh
  • 1,063
  • 1
  • 12
  • 29