gpt4 book ai didi

javascript - AngularJS JSONP TypeError : url. 替换不是函数

转载 作者:行者123 更新时间:2023-12-03 03:53:50 24 4
gpt4 key购买 nike

我正在尝试在 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 进行访问时,网络浏览器会正确显示。此 zillow API 返回 XML 数据。我怎样才能得到它作为 JSON 类型数据?

最佳答案

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

xml2json github

添加引用:

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

在你的 Controller 中,从 api 获得 xml 响应后,将其转换为 json,如下所示

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

关于javascript - AngularJS JSONP TypeError : url. 替换不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45047511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com