我是Angular JS的新手,一直困在这个问题中。Angular JS:在HTTP服务(POST)中传递输入参数
<body class="homepage" ng-app="angularApp">
<div ng-controller="myCtrl">
<ul>
@foreach ($fruits as $fruit)
<!-- looping -->
<input type="hidden" value="{{$fruit->id}}" ng-model="testParameter">
<button ng-click="testClick(testParameter)" class="adding_item_to_cart">Add to Cart</button>
@endforeach
</ul>
<p ng-bind="testBind"></p>
</div>
<script type="text/javascript">
var angularApp = angular.module('angularApp', []);
angularApp.controller('myCtrl', function($scope, $http) {
$scope.testClick = function(id) {
$http({
method : "POST",
url : "/fruits/add/".id
}).then(function addSucces(response) {
$scope.testBind =response.data;
}, function addError(response) {
$scope.testBind = "Error!";
});
}
});
</script>
</body>
我一直试图做的是发送一个参数作为HTTP请求的输入值时testClick
被调用运行POST方法,在此基础上fruit
(即换的内容循环)已被选中。 (在这个例子中,我试图运行Laravel 5.2的Route方法)。
到目前为止,我在Stackoverflow上发现了一些贴子,这些贴子显然与此任务有关。上述代码根据这些帖子的建议进行了几次修改。
我做明白这会导致一个错误,但我不知道什么是真正的错误和我需要做的,而不是。
任何意见将不胜感激!
谢谢你的回复。不幸的是,这会导致另一个错误。由于URL呈现为“http:// localhost:8000/fruits/addundefined”,因此POST方法未正确运行。 – Hiroki
所以它看起来像你的testParameter没有被设置,然后 – mindparse