0

I have an angularjs controller. I am calling a restful webservice.

app.controller('ordersCtrl', function($scope, $http, $filter) {

$scope.fetchOrders = function () {
    var url = "http://localhost:8080/blah/blah";
    // $http({method: 'POST', url : url})

    var data = $.param({"title": "kuldeep"});

    $http({method: 'POST',
            url: url,
            data: data,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
        .success(function(data) {
            console.log(data)
        });
}

I want to submit form data. So using jquery to convert object to form data. But getting the error :

$ is not defined
at Scope.$scope.fetchOrders (ordersModule.js:34)

What should I do to inject jquery inside controller. Or any alternate to make form data will be appreciated.

10
  • You should load jquery.js on DOM prior to angular.js Commented Sep 2, 2016 at 10:42
  • @Vineet It did not help. Commented Sep 2, 2016 at 10:45
  • @Vineet Why before? The application starts after the page has been loaded. Commented Sep 2, 2016 at 10:45
  • @KuldeepYadav Why you need to use jquery in your case. If you clear your scenario I can help you much in deep Commented Sep 2, 2016 at 10:46
  • @Rakeschand It has nothing to do with angular.element. The OP calls $. not $(. Commented Sep 2, 2016 at 10:47

1 Answer 1

1

No Need to Use Jquery param function use angular params serializer:

$httpParamSerializerJQLike Alternative $http params serializer that follows jQuery's param() method logic. The serializer will also sort the params alphabetically.

$http({
  url: myUrl,
  method: 'GET',
  params: myParams,
  paramSerializer: '$httpParamSerializerJQLike'
});


.controller(function($http, $httpParamSerializerJQLike) {
  //...

  $http({
    url: myUrl,
    method: 'POST',
    data: $httpParamSerializerJQLike(myData),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });

})

OR read Angular API Doc: == Click Me > $httpParamSerializerJQLike

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.