【问题标题】:How to pass parameter in Angularjs $http.post如何在 Angularjs $http.post 中传递参数
【发布时间】:2014-03-21 12:49:38
【问题描述】:

在这里,我试图将“$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'; //需要将此值传递给服务器; $http.post('javaServerlet', $scope.getCourse).success(function(data){ $scope.result = data.MainTopic; $scope.lessons = data.CourseOutline; }) }])

来自 serverlet 的 json 格式

{ "MainTopic": "Adobe", “运行时间”:“6h11min”, "Description": "课程说明来了", “课程大纲”: [ {“课程”:“第 1 课”,“标题”:“介绍 1”,“持续时间”:“31 米 27 秒”}, {“课程”:“第 2 课”,“标题”:“介绍 2”,“持续时间”:“56 米 05 秒”}, ] }

请让我知道如何实现上述 senario,我对 angularjs 很陌生。

【问题讨论】:

  • 代码是怎么失败的?您是否收到控制台错误?成功函数没有运行吗?

标签: angularjs


【解决方案1】:

你的data 应该是一对key/val,试试:

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

并且该变量将在参数course下到达服务器

【讨论】:

  • 完美!非常感谢您的快速回复。
  • 如果有多个参数,是否只是逗号分隔?
  • 非常感谢 Abhilash,tymeJV
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
  • 2023-04-06
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 2018-11-10
  • 2015-09-08
相关资源
最近更新 更多