如何在Angularjs $ http.post中传递参数 [英] How to pass parameter in Angularjs $http.post

查看:70
本文介绍了如何在Angularjs $ http.post中传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我试图将"$ scope.getCourse ='adobe'"的值传递给服务器,以便它返回相应的课程详细信息,在这里我可以从响应数据中使用ng-repeat填充列表.但是,当我同时插入"$ scope.getCourse"和servelet URL时,以下代码失败.

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;
    })  
}])

servelet的

json格式

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"},

    ]
}

请让我知道如何实现上述感觉,我对angularjs还是陌生的.

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

推荐答案

您的data应该是键/值对,请尝试:

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

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

该变量将在参数course

这篇关于如何在Angularjs $ http.post中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆