簡體   English   中英

AngularJS $ http.get()總是返回錯誤?

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

為什么下面的代碼在沒有狀態的情況下運行錯誤回調? Firefox中的Web Developer報告狀態代碼200。

有什么辦法可以調試嗎?

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

正如@robertklep指出的那樣,除非該域明確允許,否則您無法向另一個域發出ajax請求。 請參閱他提供的CORS鏈接。 一種解決方法(假設您無法控制向其請求數據的服務器的CORS策略)是在服務器支持的情況下使用JSONP請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM