簡體   English   中英

Angular.JS $ http API請求

[英]Angular.JS $http API request

我有一個奇怪的問題。 到處都是,但找不到任何解決方案。 這是我第一次使用angular.js,我正在嘗試將API中的內容添加到我的Angular App中。

成功消息有效,但(數據)為空。 如果我將GET URL從w3Schools http://www.w3schools.com/website/Customers_JSON.php更改為一個,則它可以工作。 因此,我不確定我的請求網址http://fingerzeig.ch/api/agenda/list的格式是否正確,我也不知道是否需要解碼或編碼數據。 如果我比較兩個頁面,我會發現fingerzeig URL有點不同。

任何幫助,將不勝感激。

這是我的代碼myapp.js:

var app = angular.module("MyApp", []);

app.controller("PostsCtrl", function($scope, $http) {
  $http.get('http://fingerzeig.ch/api/agenda/list').
    success(function(data, status, headers, config) {
     console.log("success!");
     console.log(data);
      $scope.posts = data;
    }).
    error(function(data, status, headers, config) {
      // log error
    });
});

HTML:

<body ng-app="MyApp">
              <div ng-controller="PostsCtrl">
                <ul ng-repeat="post in posts">
                  <li>{{post.ID}}</li>
                </ul>
            </div>
</body>

這里的鏈接:

http://www.w3schools.com/website/Customers_JSON.php =>工作

http://fingerzeig.ch/api/agenda/list =>不起作用

這些是雜項:一個帶有我要工作的請求URL。 一種帶有來自w3學校的示例url。 僅GET網址被更改。

小提琴無法正常工作: 鏈接

一個工作的人鏈接

非常感謝你!

這可能是一個跨域問題。 請注意, http://www.w3schools.com/website/Customers_JSON.php具有Access-Control-Allow-Origin: *響應標頭,而http://fingerzeig.ch/api/agenda/list沒有。

$ curl -I "http://www.w3schools.com/website/Customers_JSON.php"

如果您要發出跨域請求,則必須處理CORS。

暫無
暫無

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

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