cssjs转化为xml

JavaScript013

cssjs转化为xml,第1张

焙焙龙 2015/03/29 18:49

浏览器传统的页面获取方式是发起请求-》服务器返回数据-》浏览器根据返回的数据重新构造页面,而ajax则是浏览器中的js引擎提供了异步API,使得能够发送请求后,接收到的数据不用于直接刷新页面,而是提供了读取函数,开发者自己去处理,至于xml只是通过这种方式获取数据的格式而已,现在已经名不副实了,更多的是使用json格式。

使用代码

首先创建一个HTML页面。

<!DOCTYPE html>

<html>

<head>

<title>Convert XML to JSON In Angular JS - SibeeshPassion </title>

</head>

<body>

</body>

</html>

现在,如下添加所需的引用。

<script src="jquery-2.1.3.min.js"></script>

<script src="angular.min.js"></script>

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

你注意到了吗,我已经添加了xml2json.js文件?这就是将要转换的文件。你可以从 .google.com/p/x2js/下载文件。

现在创建一个控制器和应用程序指令,如下所示。

<div ng-app="httpApp" ng-controller="httpController">

</div>

接下来我们需要做的是添加服务。你可以按如下方式添加$http服务。

var app = angular.module('httpApp', [])

app.controller('httpController', function ($scope, $http) {

$http.get("Sitemap.xml",

{

transformResponse: function (cnv) {

var x2js = new X2JS()

var aftCnv = x2js.xml_str2json(cnv)

return aftCnv

}

})

.success(function (response) {

console.log(response)

})

})