【发布时间】:2016-12-17 19:39:06
【问题描述】:
我正在使用 Anjular js 和 Codeigniter 3 开发一个项目。我在 Angular js 中遇到了一些路由问题。我有一个包含用户详细信息列表的视图页面,并且在该列表中有一个带有 ng-click="myName()" 的标签。我已经在我的 js 文件中声明了这个函数,这个函数工作正常。
<div class="container" ng-controller="ListController">
<div class="row">
<div class="col-lg-12 text-center">
<h1>Starter Template</h1>
<p class="lead">Complete with pre-defined file paths that you won't have to change</p>
<div class="row">
<div class="col-sm-6 col-md-4 wow zoomIn" data-wow-delay="0.5s" ng-repeat="items in artists">
<div class="thumbnail">
<img ng-src="{{settings.base_url}}/application/assets/images/profile/{{items.shortname}}.jpg" alt="...">
<div class="caption">
<h3>{{items.name}}</h3>
<p>{{items.bio}}</p>
<p><a href="{{settings.base_url_controller}}auth/functionOne" class="btn btn-primary" ng-click="myName()" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
在这个函数“myName()”中,我编写了 $http.post 来获取控制器(Codeiginter 控制器)中的函数。我使用 auth.php 作为控制器,functionOne() 是要检索的函数。
var base_url_abs = window.location.origin;
var base_url = base_url_abs+'/AngularExPack/Angular_Bootstrap_CI_1/';
var base_url_controller = base_url_abs+'/AngularExPack/Angular_Bootstrap_CI_1/index.php/';
myApp.controller('ListController', ['$scope', '$http', 'settings', function($scope, $http) {
$http.get('application/datas/data.json').success(function (data){
$scope.artists = data;
});
$scope.myName = function() {
alert("hai1");
/*
$http.post(base_url_controller+'auth/functionOne/').success(function (response){
$scope.data = response
console.log(response);
//alert($scope.data);
});*/
/**/ $http({
method : 'POST',
url : base_url_controller+'auth/functionOne/',
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
$scope.data = response
console.log($scope.data);
alert($scope.data);
});
}
}]);
当我运行它时,会发生错误并在控制台中显示如下
但是,当我单击此链接以及在标签内的 href 内提供 base_url 路径时,我会得到正确的结果。
<a href="{{settings.base_url_controller}}auth/functionOne" class="btn btn-primary" ng-click="myName()" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a>
但我需要的是在这个 myName 函数内部,我必须使用 Angular js 使用 $http 访问控制器函数 functionOne()。这可能吗?
如果有人知道,请帮助我在 ng-click 中找出解决方案。如果我写错了代码,请纠正我。
谢谢。
【问题讨论】:
-
GET 有效,POST 无效。所以你的后端有问题。
-
状态码 403 响应是 Web 服务器被配置为拒绝访问的结果,因此这是一个配置问题您是否尝试替换“window.location.origin;”通过字符串?
-
@str 感谢您的评论 ReferenceError: response is not defined is shown when I used GET.
-
@DMCISSOKHO 是的,哥们仍然显示同样的错误。
-
我理解错了,你的 $http.post 不是用来发送数据的??
标签: php angularjs codeigniter http-post ngroute