Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter in Angularjs $http.post

Tags:

angularjs

Here i'm trying to pass the value of "$scope.getCourse = 'adobe'" to the server so that it returns the corresponding course details, from where i can populate list using ng-repeat from the response data. But the below code fails when i insert "$scope.getCourse" along with servelet url.

var courseApp = angular.module('courseApp', []);

courseApp.controller('courseCtrl', ['$scope', '$http', function($scope, $http){
    //$scope.getCourse = 'adobe'; //need to pass this value to the server;

    $http.post('javaServerlet', $scope.getCourse).success(function(data){
        $scope.result = data.MainTopic;
        $scope.lessons = data.CourseOutline;
    })  
}])

json format from servelet


{ 
    "MainTopic": "Adobe",
    "RunningTime": "6h11min",
    "Description": "Course Description comes here",
    "CourseOutline":
    [ 
        { "Lessons" : "Lesson 1" , "Title" : "Introduction1" , "Duration" : "31m 27s"},
        { "Lessons" : "Lesson 2" , "Title" : "Introduction2" , "Duration" : "56m 05s"},

    ]
}

Please let me know how to achieve the above senario, I'm very new to angularjs.

like image 699
Abilash Avatar asked Feb 20 '14 14:02

Abilash


People also ask

What is HTTP post in AngularJS?

AngularJS Http Post ($http. POST() services are used to send the data to a specific URL and expects the resource at that URL to handle the request that means we can say that POST method is used to Insert new data based on given URL and it is one of the shortcut method in $http service.

How do we pass data and get data using http in angular?

Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.

What is $location in AngularJS?

The $location in AngularJS basically uses a window. location service. The $location is used to read or change the URL in the browser and it is used to reflect that URL on our page. Any change made in the URL is stored in the $location service in AngularJS.

What is HTTP request in angular?

HttpRequest represents an outgoing request, including URL, method, headers, body, and other request configuration options. Instances should be assumed to be immutable. To modify a HttpRequest , the clone method should be used.


1 Answers

Your data should be a key/val pair, try:

$http.post('javaServerlet', {course: $scope.getCourse})

And that variable will get to the server under the parameter course

like image 174
tymeJV Avatar answered Sep 27 '22 19:09

tymeJV