我试图从网址获取JSON-Text-Stream(例如SOMEURL / ean.php?id = 4001513007704)。结果如下:
{
"product": {
"ean_id": "4001513007704",
"title": "Gerolsteiner Mineralwasser",
...
},
"success": true
}

正如您所看到的,这些是扫描项目的一些信息。 我使用网址搜索了实施( - > http://www.w3schools.com/angular/tryit.asp?filename=try_ng_http_get),此示例仅适用于内部网址。 Google和其他来源将变量留空。
这确实有效。它返回输入网站的HTML代码:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<p>Today's welcome message is:</p>
<h1>{{myWelcome}}</h1>
</div>
<p>The $http service requests a page on the server, and the response is set as the value of the "myWelcome" variable.</p>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("http://www.w3schools.com/angular/default.asp")
.then(function(response) {
$scope.myWelcome = response.data;
});
});
</script>
</body>
</html>
&#13;
这并不是(变量留空)。在这种情况下,我使用谷歌,但通常我们会使用其他来源!
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("http://google.com/")
.then(function(response) {
$scope.myWelcome = response.data;
});
});
</script>
&#13;
我认为这是因为网站的安全设置..
如何从网址获取数据?有没有解决方法?
感谢您提供任何帮助!
答案 0 :(得分:1)
response.data
个对象, data
只会返回一些内容。
向google.com
发送请求时收到的回复是:
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="https://www.google.co.in/">here</A>.
</BODY></HTML>
这是因为该URL提供了HTML,而不是一些JSON响应。
如果您对此进行了response.data
,那么它将是未定义的,因为 没有数据。如果您点击了具有JSON数据的正确终点,您将得到您的回复。
例如,如果您将网址更改为https://api.github.com/users/mralexgray/repos
,您就会看到数据。
答案 1 :(得分:0)
您收到此错误是因为您没有定义要包含在html中的角度模块。 尝试在w3学校中运行上述代码,并在网站中显示链接http://google.com/,如图所示
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<p>Today's welcome message is:</p>
<h1>{{myWelcome}}</h1>
</div>
<p>The $http service requests a page on the server, and the response is set as the value of the "myWelcome" variable.</p>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("http://google.com/")
.then(function(response) {
$scope.myWelcome = response.data;
});
});
</script>
</body>
</html>
&#13;