get information from sonarqube api with javascript

2,216 views
Skip to first unread message

lotje...@gmail.com

unread,
Mar 30, 2017, 8:37:50 AM3/30/17
to SonarQube
I am trying to get information from sonarQube  for a widget on TFS. 

But when I to get information with javascript I get an error:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 405.

This is the code a used:
-----------------------------------------------------------------------------------
function a(){
var xhr = new XMLHttpRequest(); 
xhr.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
  document.getElementById("demo").innerHTML = this.responseText;
}
};
xhr.open("GET","local_Url/api/authentication/validate", true);
xhr.setRequestHeader("Authorization", "Basic " + btoa("[user]:[PASSWORD]"));

xhr.send();
}
-----------------------------------------------------------------------------------
It doesn't work with the TOKEN in the URL. Or with setRequestHeader("Authorization", "Basic " + btoa("[TOKEN]]"));


Is there anyone who can help me?

Stas Vilchik

unread,
Mar 30, 2017, 10:48:17 AM3/30/17
to SonarQube, lotje...@gmail.com
Hello,

You're doing a cross-origin requests (CORS), which are not allowed by our server for security reasons.

To get some information from the server you must request api from you server, not from the browser.

Cheers,

lotje...@gmail.com

unread,
Mar 31, 2017, 5:26:35 AM3/31/17
to SonarQube, lotje...@gmail.com
what do I need to change?
We have a local sonarqube. Only to be find by entering the local network?

On IE11 the code works, but in chrome of FF ik gives me an error:



Op donderdag 30 maart 2017 16:48:17 UTC+2 schreef Stas Vilchik:

michael...@gmail.com

unread,
May 25, 2017, 11:30:14 AM5/25/17
to SonarQube, lotje...@gmail.com
Hey,
Did you ever figure out a solution for this?
I'm in the exact same scenario (trying to make a TFS widget).

Stas Vilchik

unread,
May 26, 2017, 4:44:13 AM5/26/17
to SonarQube, lotje...@gmail.com, michael...@gmail.com
Hello,

You need to enable cross-origin requests (CORS) on your sonarqube server. See here how to do it depending on your web server.

Regards,

michael...@gmail.com

unread,
May 26, 2017, 3:05:03 PM5/26/17
to SonarQube, lotje...@gmail.com, michael...@gmail.com
Hi Stas,

Thanks! I tried to do this but the server admins will not enable CORS. Do I have any other other options at this point?

Thanks,
Michael

Stas Vilchik

unread,
May 30, 2017, 3:06:43 AM5/30/17
to michael...@gmail.com, SonarQube
Hello,

If you don't enable CORS, then you have to proxy the request through the web server deployed on the same domain as your web app.

Regards,

--
You received this message because you are subscribed to a topic in the Google Groups "SonarQube" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sonarqube/FGWQWEdR_PU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/dff35c19-c367-4a8b-9520-a02b0ccd6f82%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ionut....@gmail.com

unread,
Aug 22, 2017, 1:07:53 PM8/22/17
to SonarQube, michael...@gmail.com
I don't get anymore "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 405." but now the error refers to the OPTIONS verb being used.

OPTIONS http://<sonarqube host url>/api/duplications/show?key=<project key> gives me 405

How can I overcome this issue?

Thanks,

Ionut

rahul...@gmail.com

unread,
May 8, 2018, 4:27:17 PM5/8/18
to SonarQube
Hi,

Is there any option to disable CORS via properties within sonar?
I'm using ver 6.7.

Thanks

Stas Vilchik

unread,
May 9, 2018, 3:29:52 AM5/9/18
to SonarQube
Hello,

CORS is not enabled in the SonarQube server by default. So you don't need to do anything to keep it disabled.

Cheers,
Stas
Reply all
Reply to author
Forward
0 new messages