【发布时间】:2015-08-16 12:09:46
【问题描述】:
过去几天我一直在学习 AngularJS,但是我被困在一个部分。我想要做的是允许用户输入搜索(当然是在输入小部件中),我希望搜索连接到在线 API(OpenWeatherAPI)并从在线提取 JSON 数据。然后我希望结果显示在网页中。
我已经完成了使用 AngularJS 提取 JSON 数据的源代码。我只是坚持将搜索查询连接到 API。这里是提取 REST API 的源代码:
9 var app = angular.module('oMoonShine', []);
10
11 // Note: Controller To Access Weather Data From OpenWeatherAPI (Content Type: JSON)
12 app.controller('FetchController', function ($scope, $http) {
13 $http.get("http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139", request)
14 .then(function (response) {
15 if (response.status == 200) {
16 // Note: Important To Print Your Response To Anaylse The JSON Recieved
17 // Note: For Example OpenWeatherAPI Add Additional Param So Have it
18 // Note: Like <code>$scope.info = response.data;</code> To Anaysle It
19 $scope.info = response.data;
20 }
21 });
22
23 // Note: Request Object For Extra Types
24 var request = {
25 headers: 'application/json',
26 method: 'GET'
27 };
28 });
【问题讨论】:
-
您的问题到底是什么?没有数据显示?错误信息?
-
基本上将搜索的输入连接到REST API。所以假设我正在寻找日本。我将其输入到搜索框中,然后单击提交。然后日本这个词被添加到 API 的 URL 中,然后我收到了信息。我坚持的基本上是当我点击“提交”按钮时,将其连接到 URL。
标签: javascript json angularjs rest