【问题标题】:JSONP-Call with AngularJSAngularJS 的 JSONP 调用
【发布时间】:2017-07-30 04:22:44
【问题描述】:

我正在尝试让 JSON-Call 与 AngularJS 一起工作。控制台不会抛出错误,也不会以任何方式工作。有人能帮忙吗?

$scope.click = function() {
  $http.jsonp('https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&  gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=test&callback=JSON_CALLBACK')
  .success(function(data) { 
    var results = data.query.pages;
    angular.forEach(results, function(v,k)  {
      $scope.articles.push({title: v.title, body: v.extract})
    })
  })
  .error(function () {
    alert("error");
  })
};

【问题讨论】:

  • 控制台 $scope.articles 并检查
  • 我用上面的代码创建了一个fiddle,但没有问题。
  • 也许我工作的环境有问题? codepen.io/tbswnzl/pen/Opymrz 干杯!

标签: angularjs json jsonp


【解决方案1】:

来自角度文档 您不能再使用 JSON_CALLBACK 字符串作为占位符来指定回调参数值的位置。

var url = "https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&  gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=test";
$sce.trustAsResourceUrl(url);

$scope.click = function() {
  $http.jsonp(url, {jsonpCallbackParam: 'callback'})
  .success(function(data) { 
    var results = data.query.pages;
    angular.forEach(results, function(v,k)  {
      $scope.articles.push({title: v.title, body: v.extract})
    })
  })
  .error(function () {
    alert("error");
  })
};

【讨论】:

  • 感谢您的回复!非常感激!不幸的是,它仍然没有做任何事情。我用 alert("working"); 替换了成功和错误函数中的部分。和警报(“错误”);。它现在不应该以任何一种方式显示其中一条消息吗?它根本没有做任何事情......谢谢!
  • 我想我在 jsonp 调用中有错字。
猜你喜欢
  • 2016-12-22
  • 2017-02-22
  • 2015-04-14
  • 2013-10-22
  • 1970-01-01
  • 2014-01-27
  • 2013-04-27
  • 1970-01-01
  • 2016-08-02
相关资源
最近更新 更多