角$ http.get为localhost,总是返回浏览器404 200 [英] Angular $http.get to localhost, always returns 404. 200 in browser

查看:595
本文介绍了角$ http.get为localhost,总是返回浏览器404 200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能创建在角1.2.13成功的GET请求。

  VAR getProgress =功能(){
      VAR承诺= $ HTTP({方法:GET,网址:的http://本地主机:8080 /项目/本地/一些/的getStatus'});      promise.then(函数(成功){
        警报(JSON.stringify(成功));
      },功能(错误){
        警报(JSON.stringify(错误));
      },功能(通知){
        警报(JSON.stringify(通知));
      });
  };

所以我想从我的REST Web服务获得一定的JSON。要测试该服务,我从浏览器访问它(IE9,火狐27,铬33)正常工作的所有的人。

采用了棱角分明但总是提示我有错误

以上code:

*{\"data\":\"\",\"status\":404,\"config\":{\"transformRequest\":[null],\"transformResponse\":[null],\"method\":\"GET\",\"url\":<$c$c>\"http://localhost:8080/project/local/some/getStatus\",\"headers\":{\"Accept\":\"application/json, text / plain的,的 / 的}}} *

使用的 Wireshark的我检查HTTP请求和HTTP响应,无论是从调用浏览器,并从收益角度200 Web服务的,然后选择所需JSON对象!尽管如此角度提示我用404

当我把从Firefox采用了棱角分明和调试使用FindBugs的GET请求,在控制台中我得到HTTP code 200,不过角度说,404试图debuging角的code不得要领,不能看什么奇怪的。

以上code正常工作在IE9!

任何建议都AP preciated。


解决方案

&@GeoffGenz功放;&安培; @BKM是正确的。 CORS是难辞其咎的。我想有解决问题的几种方法。


  1. 在开发过程中,不要通过从文件加载它调试自己的网页,而不是从那里使用firebugs调试部署它,您的Web服务器里面,。这是我的情况。我是从文件加载网页:/// ......和我正在到http请求:// 本地主机 ...由于协议是不同的(文件VS HTTP)请求具有不同的产地,如在的https://developer.mozilla.org/en-US/docs/Web/JavaScript/Same_origin_policy_for_JavaScript 。一旦部署在JBoss中一切正常。

  2. 第二种解决方案,我还没有尝试是使用
    JSONP角

  3. 最后一个是创建一个 ContainerResponseFilter 来改变头?


  @Provider
公共类RespFilter实现ContainerResponseFilter {
    @覆盖
    公共无效过滤器(ContainerRequestContext reqContext,ContainerResponseContext respContext)抛出IOException
        respContext.getHeaders()putSingle(访问控制允许原产地,*)。
    }
}


I can't create a successful get request in Angular 1.2.13.

  var getProgress = function() {
      var promise = $http({method: 'GET', url: 'http://localhost:8080/project/local/some/getStatus'});

      promise.then(function(success) {
        alert(JSON.stringify(success));
      }, function(error) {
        alert(JSON.stringify(error));
      }, function(notify) {
        alert(JSON.stringify(notify));
      });
  };    

So I'm trying to receive some JSON from my REST web service. To test the service, I access it from the browsers(IE9,Firefox 27, Chrome 33) works fine in all of them.

The above code using angular however, always prompts me with the error:

*{"data":"","status":404,"config":{"transformRequest":[null],"transformResponse":[null],"method":"GET","url":"http://localhost:8080/project/local/some/getStatus","headers":{"Accept":"application/json, text/plain, /"}}}*

Using wireshark I check the HTTP request and HTTP response, both calling the web service from browser and from angular returns 200, and the desired json object!! Nevertheless angular prompts me with 404.

When I make the get request from Firefox USING ANGULAR and debug using Findbugs, in the console I get HTTP Code 200, nevertheless Angular says 404. Tried debuging angular's code to no avail, can't see anything strange.

The above code works correctly in IE9!

Any suggestions are appreciated.

解决方案

@GeoffGenz && @BKM were right. CORS is to blame. I imagine there are several ways around the problem.

  1. During development, don't debug your page by loading it from file, instead deploy it, inside of your web server, and debug from there using firebugs. This was my case. I was loading the webpage from file:///...... and I was making a request to http://localhost... Because the protocol is different (file vs http) the request has different origin, as explained in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Same_origin_policy_for_JavaScript . Once deployed in JBoss everything works.
  2. A second solution, which I haven't try would be to use JSONP Angular
  3. The last one would be to create a ContainerResponseFilter to alter the headers?

@Provider
public class RespFilter implements ContainerResponseFilter {
    @Override
    public void filter(ContainerRequestContext reqContext, ContainerResponseContext respContext) throws IOException {
        respContext.getHeaders().putSingle("Access-Control-Allow-Origin", "*");
    }
}

这篇关于角$ http.get为localhost,总是返回浏览器404 200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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