正则表达式校验
1.test is not a function
在前端进行正则表达式校验时,需要使用到 Regex.test(str) 对正则和字符串进行比对。但是正则表达式是无法直接使用的,必须重新 new 一个 RegExp 生成正确格式的正则表达式对象:才能使用.test() 方法
//定义Reg 对象
const PASSWORD_REGEX = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[.@$!%*?&]).{6,20}$");
//对目标字符串进行校验
PASSWORD_REGEX.test(cipher.value.newCipher)
参考地址:后端返回正则表达式,前端test无反应以及test is not a function_.test is not a function-CSDN博客
2.post请求携带一个string 类型的参数, 前端代码和后端接收方法
如果本次post请求只需要携带一个字符串到后端,前端须是FormData格式或x-www-form-urlencoded格式,所以参数对象需要new FormData(),生成一个表单对象进行手动提交
//表单对象
let formData = new FormData();
//填充数据
formData.append('cipher', cipher.value.newCipher)
//发请求
axios.post(
'/user/shoppingUser/updateCipher',
formData
).then(data => {...})
后端呢则需要使用@RequestParam注解来接收数据。
@PostMapping("/updateCipher")
public BaseResult updateCipher(@RequestParam String cipher){
shoppingUserServiceimpl.updateCipher(cipher);
return BaseResult.ok();
}
这样才能正确接收到数据
参考地址:axios的put/post请求携带一个string 类型的参数, 前端代码和后端接收方法_this.$axios.post(的返回参数怎么接收-CSDN博客