2017-07-12 112 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);