0

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

br.julien
  • 3,420
  • 2
  • 23
  • 44
BigDataLearner
  • 1,388
  • 4
  • 19
  • 40

1 Answers1

1

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

Handling JSON Post Request in Go

Community
  • 1
  • 1
Matt Pileggi
  • 7,126
  • 4
  • 16
  • 18
  • and how to access that from the golang's http request? – BigDataLearner Mar 20 '14 at 19:36
  • This may help you there: http://stackoverflow.com/questions/15672556/handling-json-post-request-in-go – Matt Pileggi Mar 20 '14 at 19:39
  • Pillegi Thanks for the reference post. I have the below function. type category struct { category_id string } func changeCategory(res http.ResponseWriter, req *http.Request){ body, err := ioutil.ReadAll(req.Body) if err != nil { fmt.Println(err) } fmt.Println(string(body)) var t category err = json.Unmarshal(body, &t) if err != nil { fmt.Println(err) } fmt.Println(t.category_id) } It prints an empty string. – BigDataLearner Mar 20 '14 at 20:02
  • I got it,the structure type category struct { CategoryId string `json:"category_id"` } should have elements starting with Capital letter and should have json:"name of the field in json".Thanks. – BigDataLearner Mar 20 '14 at 20:28