发表于: 2018-07-02 22:29:24
1 316
今天完成的事情:
完成了任务7
明天计划的事情:
完成任务8
遇到的问题:
暂无
收获:
// 也很简单var strtime = '2014-04-23 18:55:49:123';var date = new Date(strtime);
//传入一个时间格式,如果不传入就是获取现在的时间了,这样做不兼容火狐。
// 可以这样做var date = new Date(strtime.replace(/-/g, '/'));
// 有三种方式获取,在后面会讲到三种方式的区别
time1 = date.getTime();
time2 = date.valueOf();
time3 = Date.parse(date);
/* 三种获取的区别:
第一、第二种:会精确到毫秒
第三种:只能精确到秒,毫秒将用0来代替
比如上面代码输出的结果(一眼就能看出区别): 1398250549123 1398250549123 1398250549000 */
AngularJS发送异步Get/Post请求
1 . 在页面中加入AngularJS并为页面绑定ng-app 和 ng-controller
<body ng-app="MyApp" ng-controller="MyCtrl" >...<script src="js/angular.min.js"></script>
<script src="js/sbt.js"></script>
- 1
- 2
- 3
- 4
2 . 添加必要的控件并绑定相应的事件
get:<input type="text" ng-model="param">{{param}} <br>
post: <input type="text" ng-model="user.name"><input type="text" ng-model="user.password"><br>
<button ng-click="get()">Get</button>
<button ng-click="post()">Post</button>
- 1
- 2
- 3
- 4
3 . 在JS脚本中发送进行Get/Post请求
- get
$scope.get = function () {
$http.get("/get", {params: {param: $scope.param}})
.success(function (data, header, config, status) {
console.log(data);
})
.error(function (data, header, config, status) {
console.log(data);
})
;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- get 将参数放在URL中
$scope.get = function () {
$http.get("/get?param="+$scope.param)
.success(function (data, header, config, status) {
console.log(data);
})
.error(function (data, header, config, status) {
console.log(data);
})
;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- post
$scope.post = function () {
$http.post("/post", $scope.user)
.success(function (data, header, config, status) {
console.log(data);
})
.error(function (data, header, config, status) {
console.log(data);
})
;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
4 . 由Controller处理请求并返回结果
- get
@RequestMapping("/get") @ResponseBody
public Map<String,String> get(String param) {
System.out.println("param:"+param);
response.put("state", "success");//将数据放在Map对象中
return response;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- post
@RequestMapping("/post2") @ResponseBody
public void post2(@RequestBody User user, HttpServletResponse resp) { //返回不同的http状态
if(user.getName()!=null&&!user.getName().equals("")){
resp.setStatus(200);
} else{
resp.setStatus(300);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 如果需要配置请求头部
$http({
method : "POST",
url : "/post", data : $scope.user
}).success(function(data, header, config, status) {
console.log(data);
}).error(function(data, header, config, status) {
console.log(data);
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
5 . 由JS http请求的回调函数处理并执行下一步操作
- HTML
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>Request</title></head><body ng-app="MyApp" ng-controller="MyCtrl" >get:<input type="text" ng-model="param"><br>post: <input type="text" ng-model="user.name"><input type="text" ng-model="user.password"><br>
<button ng-click="get()">Get</button>
<button ng-click="post()">Post</button></body><script src="js/angular.min.js"></script><script src="js/sbt.js"></script></html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- sbt.js
var app = angular.module("MyApp", []);
app.controller("MyCtrl", function ($scope, $http) {
$scope.get = function () {
$http.get("/get", {params: {param: $scope.param}})
.success(function (data, header, config, status) {
console.log(data);
})
.error(function (response) {
console.log(response);
})
;
} $scope.post = function () {
$http.post("/post", $scope.user)
.success(function (data, header, config, status) {
console.log(data);
})
.error(function (data, header, config, status) {
console.log(data);
})
;
}
});
评论