AngularJS jsonp成功错误

AngularJS jsonp success error

您好,我正在尝试使用 Angularjs 获取 BING MAPS API,但我在控制台中收到此错误:

TypeError: $http.jsonp(...).success is not a function

这是我的控制器

   .controller('bingMaps', ['$scope', '$http', MapController]);

    function MapController($scope, $http) {
        var vm = this;

        vm.mapsearch = function() {
            var url = "http://dev.virtualearth.net/REST/v1/Locations?callback=JSON_CALLBACK&key=MYKEY&o=json";
            $http.jsonp(url)
                .success(function(data){
                    console.log('success');
                })
                .error(function () {
                    console.log('error')
                });

        }
    }

您没有使用 jQuery。没有 success 方法。函数 returns 一个标准的承诺。它有一个 then 方法(它有两个参数,成功回调和错误回调)。

有关示例,请参阅 the documentation

您的 Bing Maps REST URL 中存在一些问题:

  • 没有回调参数。有一个jsonp参数。
  • 您没有提供要搜索的查询,因此请求会出错。

这是您的查询的修改版本:

http://dev.virtualearth.net/REST/v1/Locations?jsonp=JSON_CALLBACK&key=MYKEY&o=json&q=[your 搜索查询]

我建议查看 Bing 地图的最佳做法:

https://msdn.microsoft.com/en-us/library/dn894107.aspx

这里还有一个有用的博客 post,介绍如何使用具有不同 JavaScript 框架的 Bing Maps REST 服务:

https://blogs.bing.com/maps/2015/03/05/accessing-the-bing-maps-rest-services-from-various-javascript-frameworks/

Angular v1.6 从 JSONP 中删除了 successerror 方法:

https://github.com/angular/angular.js/blob/master/CHANGELOG.md