无法使用angularJS授权GET请求(通常在浏览器中) [英] Can't authorize GET request with angularJS (generally in browser)

查看:95
本文介绍了无法使用angularJS授权GET请求(通常在浏览器中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



这个问题让我整天疯狂:(



这是我的情况:



- 我正在尝试从我的服务器的REST API获取用户列表(xxxxxxx:8087 / im / 1 / rest / users)

- 此调用使用基本授权进行保护。

- 当我从JAVA控制台应用程序运行这个简单的代码时,我收到正确的XML

没问题





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


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







*但是当我尝试在JavaScript我无法获得

任何结果。

*我尝试了很多方法和框架(Plain JS,AngularJS,JQuery)总是有相同的结果 - 401 / Unauthorized + CORS。



我真的不知道还能尝试什么。恕我直言,我不认为它的CORS问题,因为我能够从控制台应用程序获得结果。我也支持公司代理,但这个服务器在本地网络中。



而不是代码片段(我已经尝试了所有可能的批准方法),我我想问一下这个问题是否有原因。


回顾一下我的观察结果:



*在JAVA控制台应用程序中调用GET方法 - 确定

*来自SOAP-UI的调用 - 确定

*来自多个FireFox附加组件的调用(RESTClient,HttpRequester) - 好的

*来自本地JS档案的电话 - NOK

*来自部署的JS页面的电话 - NOK



任何建议都非常感谢。



提前致谢。




* 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.

推荐答案

在您的WebAPI上启用CORS:

http://www.asp.net/web -api / overview / security / enable-cross-origin-requests-in-web-api [ ^ ]
Enable CORS on your WebAPI_:
http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api[^]


这篇关于无法使用angularJS授权GET请求(通常在浏览器中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆