2014-02-20 102 views
9

這裏我試圖將「$ scope.getCourse ='adobe'」的值傳遞給服務器,以便它返回相應的課程詳細信息,從中我可以填充使用響應數據中的ng-repeat列表。但是,當我插入「$ scope.getCourse」以及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格式

 

{ 
    "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。

+0

下怎樣的代碼會失敗?你有控制檯錯誤嗎?成功功能不運行嗎? – JeffryHouser

回答

19

data應該是一個鍵/值對,請嘗試:

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

,並且這個變量將獲得到服務器的參數course

+0

完美!非常感謝您的快速回復。 – Abilash

+0

如果有多個參數,它們只是用逗號分隔嗎? – aconkey

+0

非常感謝你Abhilash,tymeJV – Ajith

相關問題