简体   繁体   English

AngularJS $ http.get()总是返回错误?

[英]AngularJS $http.get() always returns error?

Why does the code below runs the error callback with no status? 为什么下面的代码在没有状态的情况下运行错误回调? Web Developer in Firefox reports status code 200. Firefox中的Web Developer报告状态代码200。

Is there any way to debug this? 有什么办法可以调试吗?

<meta charset="utf8">

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>

<script type="text/javascript">

    function RoomListCtrl($scope, $http) {

        $scope.getRooms = function() {

                $http({method: 'GET', url: 'http://code.angularjs.org'}).

                success(function(data, status, headers, config) {
                // this callback will be called asynchronously
                // when the response is available
                    alert('succes');
                }).

                error(function(data, status, headers, config) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
                    alert('error');
                });
        }

        $scope.submit = function() {

            this.getRooms();
        }

    }

</script>

<body ng-controller="RoomListCtrl"><form ng-submit="submit()">
    <input type="text" value="" ng-model="search" name="search" >
</form></body>

As @robertklep points out above, you can't make ajax request to another domain unless that domain explicitly allows it. 正如@robertklep指出的那样,除非该域明确允许,否则您无法向另一个域发出ajax请求。 See the CORS link he provided. 请参阅他提供的CORS链接。 One workaround to this (assuming you can't control the CORS policy for the server you're requesting data from) is to use a JSONP request if the server supports that. 一种解决方法(假设您无法控制向其请求数据的服务器的CORS策略)是在服务器支持的情况下使用JSONP请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM