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

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

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>&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>

以及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

&lt;div class=&quot;row &quot; style=&quot;margin-left:75pt;&quot;&gt;
      &lt;div class=&quot;dropdown&quot;&gt;
        &lt;label class=&quot;text-center&quot; style=&quot;margin-top:10pt;font-size:14pt;&quot;&gt; Deals on&lt;/label&gt;&amp;nbsp;
          &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;
          &lt;ul class=&quot;dropdown-menu submenu-hover-color&quot;  style=&quot;margin:0;&quot;&gt;
                &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;
          &lt;/ul&gt; 
      &lt;/div&gt;
    &lt;/div&gt;

And this code in Angularjs controller.

$scope.changeCategory = function(category_id) {
		$window.alert(category_id);
		$http({
			method : &#39;POST&#39;,
			url : &#39;/changeCategory&#39;,
			data : category_id,
			headers : { &#39;Content-Type&#39;: &#39;application/x-www-form-urlencoded&#39; } 
			// 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(&quot;error&quot;)
	    });
	}

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: { &#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:

确定