要传递给AngularJS的http数据字段的数据。

huangapple go评论119阅读模式
英文:

Data to pass in AngularJS http data field

问题

我有这段HTML代码:

  1. <div class="row " style="margin-left:75pt;">
  2. <div class="dropdown">
  3. <label class="text-center" style="margin-top:10pt;font-size:14pt;"> Deals on</label>&nbsp;
  4. <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>
  5. <ul class="dropdown-menu submenu-hover-color" style="margin:0;">
  6. <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>
  7. </ul>
  8. </div>
  9. </div>

以及AngularJS控制器中的代码:

  1. $scope.changeCategory = function(category_id) {
  2. $window.alert(category_id);
  3. $http({
  4. method : 'POST',
  5. url : '/changeCategory',
  6. data : category_id,
  7. headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
  8. // 设置头部,以便AngularJS将信息作为表单数据传递(而不是请求负载)
  9. }).success(function(data, status, headers, config) {
  10. }).error(function(data, status, headers, config) {
  11. $scope.status = status;
  12. $window.alert("error")
  13. });
  14. }

我想在Golang后端中访问AngularJS控制器函数中的category_id参数,那么我应该在http的data字段中传递什么?

提前感谢。

英文:

I have this html

  1. &lt;div class=&quot;row &quot; style=&quot;margin-left:75pt;&quot;&gt;
  2. &lt;div class=&quot;dropdown&quot;&gt;
  3. &lt;label class=&quot;text-center&quot; style=&quot;margin-top:10pt;font-size:14pt;&quot;&gt; Deals on&lt;/label&gt;&amp;nbsp;
  4. &lt;button data-toggle=&quot;dropdown&quot; class=&quot; btn dropdown-toggle dropdown-menu-hover-color text-center&quot; style=&quot;padding:0px 0px;background-color:white;&quot; href=&quot;#&quot;&gt;&lt;label style=&quot;font-size:14pt; color: #191970;&quot; &gt;{{currentCategory}}&lt;/label&gt;&amp;nbsp;&lt;b class=&quot;caret&quot;&gt;&lt;/b&gt;&lt;/button&gt;
  5. &lt;ul class=&quot;dropdown-menu submenu-hover-color&quot; style=&quot;margin:0;&quot;&gt;
  6. &lt;li&gt;&lt;a href=&quot;#&quot; id=&quot;{{page.category_id}}&quot; ng-click=&quot;changeCategory(page.category_id);&quot; ng-repeat=&quot;page in dropdownCategoryList&quot; ng-model=&quot;category_id&quot;&gt;{{page.category_name}}&lt;/a&gt;&lt;/li&gt;
  7. &lt;/ul&gt;
  8. &lt;/div&gt;
  9. &lt;/div&gt;

And this code in Angularjs controller.

  1. $scope.changeCategory = function(category_id) {
  2. $window.alert(category_id);
  3. $http({
  4. method : &#39;POST&#39;,
  5. url : &#39;/changeCategory&#39;,
  6. data : category_id,
  7. headers : { &#39;Content-Type&#39;: &#39;application/x-www-form-urlencoded&#39; }
  8. // set the headers so angular passing info as form data (not request payload)
  9. }).success(function(data, status, headers, config) {
  10. }).error(function(data, status, headers, config) {
  11. $scope.status = status;
  12. $window.alert(&quot;error&quot;)
  13. });
  14. }

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)

  1. data: { &#39;category_id&#39;: 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

huangapple
  • 本文由 发表于 2014年3月21日 03:27:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/22543188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定