无法在SAPUI5中使用REST API

时间:2018-01-23 07:38:25

标签: json rest sapui5 weather-api

我正在尝试开发一个使用第三方服务(API link here)的天气应用程序,其代码在此link中给出。当我运行应用程序时,它显示没有数据的表。它还在我下面提供的View1.Controller文件中显示为错误。

View1.controller.js

tf.decode_raw

model.js

        sap.ui.define([
        "sap/ui/core/mvc/Controller",
        "sap/ui/model/json/JSONModel"
    ], function(Controller) {
        "use strict";

        return Controller.extend("WeatherAPI.controller.View1", {


    onInit: function() {
                this._loadForecast();
            },

            _formatDate: function(date) {
                var d = new Date(date),
                    month = '' + (d.getMonth() + 1),
                    day = '' + d.getDate(),
                    year = d.getFullYear();

                if (month.length < 2) {
                    month = '0' + month;
                }
                if (day.length < 2){
                    day = '0' + day;    
                } 
                return [year, month, day].join('-');
            },

            _mapResults: function(results) {
                var oModel = this.getView().getModel();
                oModel.setProperty("/city", results.city.name);
                oModel.setProperty("/country", results.city.country);

                var aForecastResults = [];
                for (var i = 0; i < results.list.length; i++) {
                    var oTemp = results.list[i].temp;
                    var date = this._formatDate(results.list[i].dt * 1000);
                    aForecastResults.push({
                        date: date,
                        temp: oTemp.day,
                        units: "Celsius",
                        humidity: results.list[i].humidity
                    });
                }

                oModel.setProperty("/items", aForecastResults);
            },

            _loadForecast: function() {
                var oView = this.getView();
                var oParams = {
                    q: "London",  // Get the weather in london
                    units: "metric", 
                    appid: "fa2a1fb5db6debe044492728ca66a8f8",  // replace with your API key
                    cnt: 16,  // get weather for the next 16 days
                    mode: "json"  // get it in JSON format 
                };
                var sUrl = "/OpenWeather/data/2.5/forecast/daily";
                oView.setBusy(true);

                var self = this;

                $.get(sUrl, oParams)
                    .done(function(results) {
                        oView.setBusy(false);
                        self._mapResults(results);
                    })
                    .fail(function(err) {
                        oView.setBusy(false);
                        if (err !== undefined) {
                            var oErrorResponse = $.parseJSON(err.responseText); /*error : Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>) */
                            sap.m.MessageToast.show(oErrorResponse.message, {
                                duration: 6000
                            });
                        } else {
                            sap.m.MessageToast.show("Unknown error!");
                        }
                    });
            }
        });
    });

新app.json

        sap.ui.define([
        "sap/ui/model/json/JSONModel",
        "sap/ui/Device"
    ], function(JSONModel, Device) {
        "use strict";

        return {

            createDeviceModel: function() {
                var oModel = new JSONModel(Device);
                oModel.setDefaultBindingMode("OneWay");
                return oModel;
            },

            createAPIModel: function() {
                var oModel = new JSONModel();
                oModel.setData({
                    city: "",
                    country: "",
                    cols: [{
                        name: "Date"
                    }, {
                        name: "Day Temperature"
                    }, {
                        name: "Units"
                    },{
                        name: "humidity"
                    }],
                    items: []
                });
                return oModel;          
            }


        };
    });

请帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

如果您的应用在域外调用API,则会遇到CORS问题。也许是这样的。您的网络开发人员窗口将帮助您解决这个问题。