运行第一个angular应用遇到的坑

今天尝试了学习http://www.runoob.com/angularjs/angularjs-tables.html
里边的http.get( )遇到了很多坑,主要是面临跨域的问题:但解决办法没有实现,今天能成功是靠着放在本地实现的
首先在jsp页面设置basePath 这个是json文件的地址。
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/resources/ajax/test.json";
%>

然后将basePath放在<script>脚本当中即可:
一定得放在单引号当中,否则出错。
<div ng-app="myApp" ng-controller="customersCtrl">

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.title }}</td>
    <td>{{ x.url }}</td>
    <td>{{ x.abs }}</td>
  </tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl',function($scope, $http) {
    $http.get('**<%=basePath%>**')
    .then(function (result) {
            console.log(result.data);
        $scope.names = result.data.feed.entry;
    });
});
</script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容