【问题标题】:Angular 1.6 $http.jsonp while using the google sheets APIAngular 1.6 $http.jsonp 使用谷歌表格 API
【发布时间】:2017-11-07 19:16:42
【问题描述】:

Angular 1.6 的 $http.jsonp 不能很好地与 google sheet 的 API 配合使用:

我正在尝试从谷歌表格中获取然后获取我的数据,使用以下内容:

var callback;
app.controller("meetingsTable", function ($scope, $http, $sce) {

var url = "http://spreadsheets.google.com/a/google.com/tq";
var trustedUrl = $sce.trustAsResourceUrl(url);
var key = 'MY_KEY';
var tq = 'select%20*%20limit%2010';
var tqx = 'responseHandler:callback';
var params = {
    key: key,
    tq: tq,
    status: 'ok',
    tqx: tqx
};

callback = function (response) {
    console.log(response); // entering here, not to the promise
    return response;
}


    $http.jsonp(trustedUrl, { params: params }).then(function (response) {
        console.log(response);
        retrun;
        //success things go here
    }, function (response) {
        //error things go here
    });
});

我成功地通过使用函数(回调)和 vnila js 从工作表中获取数据,当我尝试使用 angular 时,我在源代码中得到了一个“google.visualization.Query.setResponse”对象,出现控制台错误:Uncaught ReferenceError: google is not defined。

最烦人的事情 - 承诺没有收到响应,我无法更新我的表的值 ansyc。 我尝试了我能想到的一切(以及stackoverflow中的每一个建议), 我尝试过的事情:

  1. 按原样传递 url,不带参数,因为 $sce.trustAsResourceUrl 需要整个 url。
  2. 在没有 $sce 的情况下通过(适用于 vanila js,不在此处)。
  3. 将我的承诺成功函数命名为“回调”。
  4. 检查工作表 API 中的所有值是否都在此处(同样适用于 vanila)。
  5. 在 Promise 中调用“回调”,将其作为函数输入到 Promise 中。
  6. 在一个返回响应的函数中获取所有 jsonp,有&没有回调函数。
  7. 从“tqx=responseHandler:callback”参数中删除所有回调。
  8. 在 tqx 参数中将 promise 作为回调传递。
  9. 使用 1.5
  10. 使用 vanila js 发出请求,然后将其传递给控制器​​(不起作用)。

如果我记得更多的东西,我会在下面更新。

请问,有没有人知道问题出在哪里? 万分感激, 谢谢, 约夫。

【问题讨论】:

    标签: javascript angularjs jsonp angular-promise google-sheets-api


    【解决方案1】:

    回答我自己的问题:

    如果你们有同样的问题,请使用 Angular 的 $scope.$apply 属性。这是 Angular API 中记录不充分的属性,所以这里有一个很好的 guide 对于如何使用 $apply,有一个很好的例子。 我的实现:

    $scope.tableContentData;
    callback = function (response) {
        $scope.$apply(function () {
            $scope.tableContentData = response;
        });
    };
    $http.jsonp(trustedUrl).then(function () {
            //success stuff
        }, function () {
            //error stuff
        });
    

    当我在控制器外声明回调时。

    这是一场噩梦。

    无论如何感谢您的投票!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 2017-11-28
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      相关资源
      最近更新 更多