0

I have a Rest API developed in spring boot.It is running on port 9090.I have to consume the api from my website to get some data using angluarjs.The website is hosted in a shared hosting server which port is 80.Now there occurs problem in consuming the API from 80 port to a different port 9090.

I also tried using curl from php there is same issue occurs.

Here is my API : https://****url****:9090/aubwebsite-0.0.1-SNAPSHOT/api/document/alldocuments

AngularJs Controller is :

$scope.getDocuments = function(){
                var search  = {
                    pageNo : 1,
                    studentId:"",
                    studentName:"",
                    rowPerPage:11
                };
                $http({
                    method: "POST",
                    url: 'http://****url****:9090/aubwebsite-0.0.1-SNAPSHOT/api/document/alldocuments',
                    data: $httpParamSerializerJQLike(search),
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                }).then(function(response) {
                    console.log(response.data.documentList);
                    if(response.data.documentList === undefined || response.data.documentList.length == 0){
                        $scope.documents = [];
                    }else {
                        $scope.documents = response.data.documentList;
                        $scope.quantity = response.data.itemQuantity;
                    }
                    console.log($scope.documents,'All documents List');
                    console.log(response.data);
                });
            }

From the above call there is no api call take place.

How to consume an API from website whereas the port of the API and website deployed are different?

1 Answers1

0

You need to implement Cross-Origin Resource Sharing, known as CORS.

At its simplest, implement something like the following as a response to OPTIONS request:

Access-Control-Allow-Origin: http://your-origin-website
Access-Control-Allow-Methods: POST, OPTIONS
Access-Control-Allow-Headers: Content-Type
Access-Control-Max-Age: 86400

However, you claim you have the same issue when using curl from PHP. That shouldn't happen.

Some further discussion on this site related to CORS:

eis
  • 51,991
  • 13
  • 150
  • 199
  • Another important note that my api server does not provide a secure connection i.e it is accessed with http://. I also used http in my ajax call for the api url.But in networks it seems that it is calling with https://.Website server provides secure connection.please find the following snapshot : https://drive.google.com/open?id=1uTFu8HwftSmM-EJ7QK96XGxy5RIXfOAa. so how to call with http instead of https from a secured server. – Mahbub Ul Alam Shohag Aug 19 '19 at 10:41