-
Notifications
You must be signed in to change notification settings - Fork 38
/
blog.js
189 lines (183 loc) · 5.73 KB
/
blog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
$(document).ready(function () {
//注册
$("register-form").validate({
rules: {
username: {
required: true,
rangelength: [5, 10]
},
password: {
required: true,
rangelength: [5, 10]
},
repassword: {
required: true,
rangelength: [5, 10],
equalTo: "#register-password"
}
},
messages: {
username: {
required: "请输入用户名",
rangelength: "用户名必须是5-10位"
},
password: {
required: "请输入密码",
rangelength: "密码必须是5-10位"
},
repassword: {
required: "请确认密码",
rangelength: "密码必须是5-10位",
equalTo: "两次输入的密码必须相等"
}
},
submitHandler: function (form) {
var urlStr = "/register";
// alert("urlStr:"+urlStr)
$(form).ajaxSubmit({
url: urlStr,
type: "post",
dataType: "json",
success: function (data, status) {
alert("data:" + data.message)
if (data.code == 1) {
setTimeout(function () {
window.location.href = "/login"
}, 1000)
}
},
err: function (data, status) {
alert("err:" + data.message + ":" + status)
}
})
}
});
//登录
$("#login-form").validate({
rules: {
username: {
required: true,
rangelength: [5, 10]
},
password: {
required: true,
rangelength: [5, 10]
}
},
messages: {
username: {
required: "请输入用户名",
rangelength: "用户名必须是5-10位"
},
password: {
required: "请输入密码",
rangelength: "密码必须是5-10位"
}
},
submitHandler: function (form) {
var urlStr = "/login"
alert("urlStr:" + urlStr)
$(form).ajaxSubmit({
url: urlStr,
type: "post",
dataType: "json",
success: function (data, status) {
alert("data:" + data.message + ":" + status)
if (data.code == 1) {
setTimeout(function () {
window.location.href = "/"
}, 1000)
}
},
error: function (data, status) {
alert("err:" + data.message + ":" + status)
}
});
}
});
//修改和添加文章的表单
$("#write-art-form").validate({
rules: {
title: "required",
tags: "required",
short: {
required: true,
minlength: 2
},
content: {
required: true,
minlength: 2
}
},
messages: {
title: "请输入标题",
tags: "请输入标签",
short: {
required: "请输入简介",
minlength: "简介内容最少两个字符"
},
content: {
required: "请输入文章内容",
minlength: "文章内容最少两个字符"
}
},
submitHandler: function (form) {
alert("hello")
var urlStr = "/article/add";
//判断文章id确定提交的表单的服务器地址
//若id大于零,说明是修改文章
var artId = $("#write-article-id").val();
alert("artId:" + artId);
if (artId > 0) {
urlStr = "/article/update"
}
alert("urlStr:" + urlStr);
$(form).ajaxSubmit({
url: urlStr,
type: "post",
dataType: "json",
success: function (data, status) {
alert(":data:" + data.message);
setTimeout(function () {
window.location.href = "/"
}, 1000)
},
error: function (data, status) {
alert("err:" + data.message + ":" + status)
}
});
}
});
//文件
$("#album-upload-button").click(function () {
var filedata = $("#album-upload-file").val();
if (filedata.length <= 0) {
alert("请选择文件!");
return
}
//文件上传通过Formdata去储存文件的数据
var data = new FormData()
data.append("upload", $("#album-upload-file")[0].files[0]);
alert(data)
var urlStr = "/upload"
$.ajax({
url: urlStr,
type: "post",
dataType: "json",
contentType: false,
data: data,
processData: false,
success: function (data, status) {
alert(":data:" + data.message);
if (data.code == 1) {
setTimeout(function () {
window.location.href = "/album"
}, 1000)
}
},
error: function (data, status) {
alert("err:" + data.message + ":" + status)
}
})
})
});