Facing CORS policy issue when calling the SonarQube API's using $http in angularjs

sonarqube

(Ankita) #1
  1. SonarQube version - 7.3.0.15553
  2. Trying to achieve - I am trying to access the SonarQube API. Making an HTTP GET request to the SonarQube server hosted internally. Issue is unable to get the json body as CORS issue on server side message comes when the request is made.
  3. How far is achieved - Can make http get request using postman but not programmatically. Means using the angularjs code.

Note - In SonarQube server already set the ‘Access-Control-Allow-Origin’ : ‘*’ .


(Grégoire Aubert) #3

Hello,
If the Access-Control-Allow-Origin: * header is correctly set on the server it should work.
Are you sure you are correctly doing the request? For example, if you are using fetch, are you correctly enabling the cors mode?


(Ankita) #4

Hi Aubert,

Thanks for replying :slight_smile:
Well yes, I have set the Access-Control-Allow-Origin: * header correctly. My SonarQube is hosted on the virtual machine in Azure (cloud) and I am requesting that server from Angularjs application running locally(i.e., on my system).

Error Message coming
**Access to XMLHttpRequest at ‘http://SonarQubeUrl’ from origin ‘http://127.0.0.1:8080’ has been blocked by CORS policy: **
Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

AngularJs Code
app.controller(“sonarQubeCtrl”, [’$scope’, ‘$http’, function($scope, $http) {
$http({
method: ‘GET’,
url: “SonarQubeUrlHere”,
headers: {“Authorization” : “Basic auth_token”}
})
.then(function(response) {
console.log(“success”);
$scope.values = response;
}, function errorCallback(error) {
console.log(“error came”); // error is thrown
console.log(error);
})
}]);