2017-07-12 43 views
0

我想在角JS中使用zillow api获取URL。AngularJS JSONP TypeError:url.replace不是函数

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类型的数据?

+0

您问题**中的网址不是**支持JSONP的资源 – Phil

回答

1

您可以使用xml2json。从这里得到它:

xml2json github

添加refrance:

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

,并在你的控制器,你从API XML响应后,将其转换成JSON这样

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