簡體   English   中英

無法使用http.get訪問Json。 狀態出來是-1

[英]Unable to access Json using http.get . status is coming out is -1

我是AngularJS的新手。 在我的代碼中,我試圖使用$http.get訪問json數據,但是它將出錯並且狀態返回為-1。

怎么了?

RecordApp.factory('recordaccess', ['$http', function($http) {
    $http.get('http://someurlforjson')
        .success(function(data) {
            return data;
        })
        .error(function(data, status, headers, config) {
            alert("Error occurred. Status: " + status);
        });
}]);

$ http舊式的Promise方法successerror已被棄用。 使用標准的then方法代替:

RecordApp.factory('recordaccess', ['$http', function($http) {
    return $http.get('http://someurlforjson')
        .then(function successCallback(response) {
            // this callback will be called asynchronously
            // when the response is available
            return response.data;
        },
        function errorCallback(response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
            alert("Error occurred. Status: " + reponse.status + " - " + response.statusText);
        });
}]);

您可以在Angular Docs中找到更多信息。

如果這沒有幫助,則JSON路徑必定有問題(該路徑不正確或不允許您訪問它,例如CORS)。

暫無
暫無

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

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