英文:
Data to pass in AngularJS http data field
问题
我有这段HTML代码:
<div class="row " style="margin-left:75pt;">
<div class="dropdown">
<label class="text-center" style="margin-top:10pt;font-size:14pt;"> Deals on</label>
<button data-toggle="dropdown" class=" btn dropdown-toggle dropdown-menu-hover-color text-center" style="padding:0px 0px;background-color:white;" href="#"><label style="font-size:14pt; color: #191970;" >{{currentCategory}}</label> <b class="caret"></b></button>
<ul class="dropdown-menu submenu-hover-color" style="margin:0;">
<li><a href="#" id="{{page.category_id}}" ng-click="changeCategory(page.category_id);" ng-repeat="page in dropdownCategoryList" ng-model="category_id">{{page.category_name}}</a></li>
</ul>
</div>
</div>
以及AngularJS控制器中的代码:
$scope.changeCategory = function(category_id) {
$window.alert(category_id);
$http({
method : 'POST',
url : '/changeCategory',
data : category_id,
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
// 设置头部,以便AngularJS将信息作为表单数据传递(而不是请求负载)
}).success(function(data, status, headers, config) {
}).error(function(data, status, headers, config) {
$scope.status = status;
$window.alert("error")
});
}
我想在Golang后端中访问AngularJS控制器函数中的category_id参数,那么我应该在http的data字段中传递什么?
提前感谢。
英文:
I have this html
<div class="row " style="margin-left:75pt;">
<div class="dropdown">
<label class="text-center" style="margin-top:10pt;font-size:14pt;"> Deals on</label>&nbsp;
<button data-toggle="dropdown" class=" btn dropdown-toggle dropdown-menu-hover-color text-center" style="padding:0px 0px;background-color:white;" href="#"><label style="font-size:14pt; color: #191970;" >{{currentCategory}}</label>&nbsp;<b class="caret"></b></button>
<ul class="dropdown-menu submenu-hover-color" style="margin:0;">
<li><a href="#" id="{{page.category_id}}" ng-click="changeCategory(page.category_id);" ng-repeat="page in dropdownCategoryList" ng-model="category_id">{{page.category_name}}</a></li>
</ul>
</div>
</div>
And this code in Angularjs controller.
$scope.changeCategory = function(category_id) {
$window.alert(category_id);
$http({
method : 'POST',
url : '/changeCategory',
data : category_id,
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
// set the headers so angular passing info as form data (not request payload)
}).success(function(data, status, headers, config) {
}).error(function(data, status, headers, config) {
$scope.status = status;
$window.alert("error")
});
}
I want to access the category_id parameter of the controller function in AngularJS in Golang at backend,so what should i pass in http's data field.
Thanks in advance
答案1
得分: 1
data是一个键值对映射(实质上是请求参数)。
data: {'category_id': category_id}
然后你的POST请求应该有一个名为"category_id"的参数。
关于在Go中处理JSON的问题,可以参考以下链接:
https://stackoverflow.com/questions/15672556/handling-json-post-request-in-go
英文:
data is a mapping of key/values (requests params essentially)
data: { 'category_id': category_id }
Then your POST should have a param named "category_id"
and check this post about handling JSON in go
https://stackoverflow.com/questions/15672556/handling-json-post-request-in-go
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论