AngularJS JSONP TypeError:url.replace不是一个函数[重复]

Posted

技术标签:

【中文标题】AngularJS JSONP TypeError:url.replace不是一个函数[重复]【英文标题】:AngularJS JSONP TypeError: url.replace is not a function [duplicate] 【发布时间】:2017-12-16 06:45:14 【问题描述】:

我正在尝试在 Angular JS 中使用 zillow api 获取 URL。

angular.module('account.settings').controller('AccountSettingsCtrl', ['$scope', '$http', '$sce',
  function($scope, $http, $sce) 

    var getZillowURL = function() 
      var url = "http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=X1-ZWz1ft20wfj30r_94p25&address=2114+Bigelow+Ave&citystatezip=Seattle%2C+WA";
      var trustedUrl = $sce.trustAsResourceUrl(url);
      $http.jsonp(trustedUrl, 
          jsonpCallbackParam: 'callback'
        )
        .success(function(result) 
          $scope.mortgageLocation = result;
          console.log('success!');
          console.log(result);
        )
        .error(function(data, status) 
          console.log('error!');
          console.log(data);
        );
    ;
    getZillowURL();
  
]);

但是 JSONP 返回错误。 当我使用用作 JSONP 参数的 URL 访问时,webbrowser 正确显示。 此 zillow API 返回 XML 数据。 我怎样才能将其作为 JSON 类型的数据获取?

【问题讨论】:

您问题中的网址不是支持JSONP的资源 【参考方案1】:

您可以使用 xml2json。从这里获取:

xml2json github

添加重发:

<script src="xml2json.js"></script>

并且在您的控制器中,在您从 api 获得 xml 响应后,将其转换为 json,如下所示

  var x2js = new X2JS();
  var jsonResponse= x2js.xml_str2json(your_xml);

【讨论】:

以上是关于AngularJS JSONP TypeError:url.replace不是一个函数[重复]的主要内容,如果未能解决你的问题,请参考以下文章

AngularJS中的JSONP实践

在 AngularJS 中访问 JSONP 请求的真实 URL

带有 jsonp 的 AngularJS 资源服务失败

在 AngularJS 中设置 JSONP 回调函数

angularjs - 返回 JSONP 时不执行 HTTPpromise

如何从 angularjs 中的 JSONP 错误回调中获取数据?