完成的任务
- 对session进行配置
- 设置拦截器,管理拦截器,确保要先登录过后才能发布商品
- control层url请求登录时为session加个id属性,商品发布api从session中得到user的id
- 发布商品页面的开发,动态渲染
- 用JavaScript获得表单数据,存储到对象属性当中,用fetch进行数据对提交以及响应
解决的问题
设置拦截器时,除了对登录注册页面不拦截,还要对静态文件,登录注册的api也要不拦截
对JavaScript对复选框按钮值的获取
用数组来存储,根据按钮节点checked为选中状态,来遍历存储值
代码如下,方便下次套用
const regButton = document.querySelector('.button');
const checkbox=document.getElementsByName('category');
regButton.addEventListener('click', function() {
let categories=[];
for (k in checkbox){
if (checkbox[k].checked){
let category={};
category.id=checkbox[k].value;
categories.push(category);
}
}
// 取得输入框的值
let data = {
"name": document.getElementById('name').value,
"description": document.getElementById('description').value,
"categories": categories,
// "images": document.getElementById('images').value,
// "detail": document.getElementById('detail').value,
"price": document.getElementById('price').value,
"stock": document.getElementById('stock').value,
"status":document.getElementById('status').value,
};
fetch(
'/product/pub/api',
{
body: JSON.stringify(data),
cache: 'no-cache',
headers: {
'content-type': 'application/json'
},
method: 'POST'
}
)
.then(function(response) {
return response.json();
})
.then(function(result) {
console.log("data is :" + JSON.stringify(result))
if (result.isSuccess == true || result.success == true) {
alert("发布成功");
// 这里加入跳转语句跳转到登录页面
window.location.href='/success';
}else {
alert("发布失败"+" "+result.message)
}
});
});