Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ';TypeError:undefined不是函数';从github api中以Angular方式提取json数据时?_Javascript_Json_Angularjs - Fatal编程技术网

Javascript ';TypeError:undefined不是函数';从github api中以Angular方式提取json数据时?

Javascript ';TypeError:undefined不是函数';从github api中以Angular方式提取json数据时?,javascript,json,angularjs,Javascript,Json,Angularjs,我正在开发一个单页Angular应用程序,并试图从github api中提取数据,该api可以通过以下方式访问我的github数据: 我得到一个错误:“TypeError:undefined不是一个函数” 我想做的只是拉出我的登录用户名“payam10”并将其放在h1html元素中。请参阅下面的代码 <html ng-app="myapp"> <head> <title>Angular</title> <script src="http://

我正在开发一个单页Angular应用程序,并试图从github api中提取数据,该api可以通过以下方式访问我的github数据:

我得到一个错误:“TypeError:undefined不是一个函数”

我想做的只是拉出我的登录用户名“payam10”并将其放在h1html元素中。请参阅下面的代码

<html ng-app="myapp">
<head>
<title>Angular</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body>
 <div ng-controller="githubController">
    <h1>{{ user.name }}</h1>
 </div>

<script>
    var myapp = angular.module('myapp', []);

    myapp.controller('githubController', ['$scope', '$http', function($scope, $http) {

        $scope.user = {}

        $scope.user.name = '';

        return $http.jsonp
                        .get('http://api.github.com/users/payam10')
                        .success(function (data, status, headers, config) {
                          // successful data retrieval
                          $scope.user.name = data.login
                        })
                        .error(function (data, status, headers, config) {
                          // successful data retrieval
                          $scope.user.name = "Sorry, we were unable to retieve your data."
                        })

    }]);

 </script>

有棱角的
{{user.name}
var myapp=angular.module('myapp',[]);
myapp.controller('githubController',['$scope','$http',函数($scope,$http){
$scope.user={}
$scope.user.name='';
返回$http.jsonp
.get('http://api.github.com/users/payam10')
.success(函数(数据、状态、标题、配置){
//成功的数据检索
$scope.user.name=data.login
})
.error(函数(数据、状态、标题、配置){
//成功的数据检索
$scope.user.name=“很抱歉,我们无法重新查看您的数据。”
})
}]);

问题在于这是一个跨域请求。出于安全原因,浏览器会阻止这样做


有一些变通办法。您可以查看CORS、JSONP或反向代理。

您的代码中有一个错误:

$http.jsonp.get('url')…
应该是
$http.jsonp('url')…

在回调中,您应该分配
$scope.user=data.data
以获取完整对象

请注意
data.data
,因为当指定回调时,GitHub API似乎将其json封装在另一层中

使用JSONP,您还必须在url中定义回调(我以为Angular是自动执行的,但它没有):

http://api.github.com/users/payam10?callback=JSON_CALLBACK


你怎么知道的?我怎样才能修复它使它工作?我把它改成$http.jsonp,它就工作了!但是我无法访问刚刚获取的数据{{user.name},因为您只在回调中分配
$scope.user.name
。请改为尝试
$scope.user=data
。另外,它是
$http.jsonp()
而不是
$http.jsonp.get()
我将$scope.user.name=data.login更改为$scope.user=data,但仍然没有定义。我不应该以任何一种方式获取数据吗?你能告诉我怎么把它固定在小提琴上吗?我试过你说的,但我也犯了同样的错误。它充满了棱角,我现在明白了。为什么您必须在“data.data.login”中执行两次数据处理?当您使用回调调用GitHub API时,它会将json封装在另一层中。尝试通过回调转到新的url。你说的新url是什么意思?这一个与这一个相比