【发布时间】:2015-09-08 09:57:48
【问题描述】:
我正在尝试使用 $http.post() 将数据发送到服务器。我写了一些代码,但它什么也没显示。我只想发送一个字符串变量并从 java 页面打印它。
<body ng-controller="HelloCtrl">
<script type="text/javascript">
var app=angular.module("MyApp",[]);
app.controller("HelloCtrl", function($scope, $http) {
$scope.name="Subhajyoti";
$scope.showName= function(){
$http.post('insertEntry',$scope.name)
.success(function(data){
alert(data);
})
.error(function(data){
alert("Error");
})
}
});
</script>
</body>
这是我的 java 页面代码..
@Controller
public class Rules {
@RequestMapping(value="insertEntry", method=RequestMethod.POST)
public @ResponseBody String insertEntry(@RequestParam String name){
String val=name;
System.out.println(val);
JSONObject json = new JSONObject();
String message = "Success";
json.put("message", message);
return json.toString();
}
}
我正在与 spring mvc 交互 angularjs。请帮我举个例子。我是新手。
【问题讨论】:
-
确保把 url 不仅仅是 insertEntry
-
您使用的是什么 angularJS 版本?
.success和.error已弃用。 -
请帮我编写java页面编码。
标签: java ajax angularjs spring