Click here to Skip to main content
15,881,744 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

This problem is driving me crazy whole day :(

Here is my situation:

- I'm trying to get user list from REST API of my server (xxxxxxx:8087/im/1/rest/users)
- This call is secured with Basic authorization.
- When I run this simple code from JAVA console app, I receive correct XML with
no problems


Java
public static void main(String[] args) throws IOException {

String url = "http://xxxxxxxxxxx:8087/im/1/rest/users";
URL obj = new URL(url);

HttpURLConnection con = (HttpURLConnection) obj.openConnection();


con.setRequestMethod("GET");
con.setRequestProperty("Authorization", "Basic ZGVmYXVsdFxhZG1pbjpBZG1pbg==");

int responseCode = con.getResponseCode();
Map<String, List<String>> map = con.getHeaderFields();


BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}

in.close();

System.out.println(response.toString());
}



* However when I try to do the same within JavaScript I can't get
any results.
* I've tried so many approaches and frameworks (Plain JS, AngularJS, JQuery) always with same result - 401/Unauthorized + CORS.

I really don't know what else to try. IMHO I don't think its CORS problem, since I'm able to get results from console app. Also I'm behind corporate proxy, but this server is in local network.

Rather than code snippet(I've tried every possible approved method from here), I'd like to ask whether there could be some cause to this problem.

Recap of my observations:

* Call of GET method within JAVA console app - OK
* Call from SOAP-UI - OK
* Call from several FireFox add-ons (RESTClient, HttpRequester) - OK
* Call from local JS file - NOK
* Call from deployed JS page - NOK

Any suggestion is greatly appreciated.

Thanks in advance.
Posted
Comments
stibee 30-Jul-15 9:30am    
Pls. can you post the whole error message and your javascript code?
Milan Městka 30-Jul-15 9:50am    
Hello stibee,

Thank you for your response.

Interestingly, it seems, that internet explorer is able to execute JS with correct result.

FF and Chrome does not.

Err messages are:

* Failed to load resource: the server responded with a status of 401 (Unauthorized)
* XMLHttpRequest cannot load http://xxxxxxx:8087/im/1/rest/users. 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 401.


As mentioned before, server indeed does not have Control-Allow-Origin parameter set. But that would took out java console app as well right?

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900