【问题标题】:angularjs http remote api failing to loadangularjs http远程api无法加载
【发布时间】:2014-06-26 13:40:32
【问题描述】:

有人可以在他们的本地主机上运行此代码吗?我一直在试图弄清楚为什么第一个 GET 返回一个空响应,而第二个 GET 返回预期的响应,但表明它的格式不正确。任何帮助将不胜感激。

<!DOCTYPE html>
<html lang="en-gb" ng-app="app">
<body>
  <div ng-controller="mainController"></div>

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
  <script>
    var app = angular.module('app',[]).controller('mainController', function($scope, $http){

    $scope.baseUrl = "http://api.nestoria.co.uk/api?action=echo&encoding=json&foo=bar";
    //$scope.baseUrl = 'http://www.iNorthwind.com/Service1.svc/getAllCustomers';

    // Works but no response content
    $http({method: 'GET', url: $scope.baseUrl})
     .success(function(data,status){
      console.log( data);
     });

    // Works but console states malformed jsonp, but has response content.      
   $http({method: 'jsonp', url: $scope.baseUrl + "&callback=JSON_CALLBACK'})
     .success(function(data,status){
      console.log( data);
    });   
  });
 </script>
</body>
</html>

谢谢

【问题讨论】:

    标签: angularjs api http get


    【解决方案1】:

    在第二个中,您在 CALLBACK 之后的引用不匹配

       $http({method: 'jsonp', url: $scope.baseUrl + "&callback=JSON_CALLBACK"})
         .success(function(data,status){
          console.log( data);
        });   
    

    【讨论】:

    • 谢谢,治愈了。也许我应该多睡一会儿,晚上少写点代码。
    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2020-07-31
    • 1970-01-01
    • 2015-04-15
    相关资源
    最近更新 更多