Angular.JS $ HTTP API请求 [英] Angular.JS $http API request

查看:146
本文介绍了Angular.JS $ HTTP API请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种奇怪的问题。到处找,但找不到任何解决方案。这是我第一次与angular.js,我想从一个API的内容在我的角度应用。

I have kind of a strange problem. Was looking everywhere but cannot find any solution. It is my first time with angular.js and I'm trying to get content from an API into my Angular App.

成功信息工作,但是(数据)是空的。如果我改变了GET URL到一个来自W3Schools的 http://www.w3schools.com/website/ Customers_JSON.php 的工作原理。所以,我不知道如果我的请求URL http://fingerzeig.ch/api/agenda/list格式正确我也不知道我是否需要取消code或EN code中的数据。如果我两页比较我注意到fingerzeig网址是有点不同。

The Success Message works but the (data) is empty. If I change the GET URL to the one from w3Schools http://www.w3schools.com/website/Customers_JSON.php it works. So I'm not sure if my request URL http://fingerzeig.ch/api/agenda/list is formatted properly neither do i know if i need to decode or encode the data. If i compare the two pages i noticed that the fingerzeig URL is a little bit different.

任何帮助将是AP preciated。

Any help would be appreciated.

下面是我的code myapp.js:

Here is my code 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 =>不工作

下面是fiddels:
一位与我有我的请求URL想拍的工作。一位来自W3学校的例子网址。只有GET URL被改变。

Here are the fiddels: One with my request url with i want to make work. One with the example url from w3 schools. Only the GET url's are changed.

小提琴不工作:

Fiddle not working: link

谁的作品链接

非常感谢你!

推荐答案

这可能是一个跨域的问题。需要注意的是 http://www.w3schools.com/website/Customers_JSON.php 访问控制允许来源:* 响应头和 http://fingerzeig.ch/api/agenda/list 没有。​​

This is probably a cross-domain issue. Note that http://www.w3schools.com/website/Customers_JSON.php has the Access-Control-Allow-Origin: * response header and http://fingerzeig.ch/api/agenda/list does not.

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

如果你让你不得不处理CORS跨域请求。

If you're making a cross-domain request you have to deal with CORS.

这篇关于Angular.JS $ HTTP API请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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