进入大三很久没有写前端了,今天开始记录一下血泪史
坑1:用post方法传json数据
var param={name:userName,pwd:userPwd}; 是传递给后端的json数据
以为和postman里写的格式一样就好了,然鹅,一直400错误,后来找了好久才知道要用 JSON.stringify(param)
具体操作看下图
$("#loginBtn").click(function(){ var userName=$("#inputName").val(); var userPwd=$("#inputPwd").val(); var param={name:userName,pwd:userPwd}; console.log(userName+" "+userPwd); console.log(JSON.stringify(param)); $.ajax({ type:"POST", contentType : "application/json;charset=utf-8", url:"http://localhost:8080/api/student/login", dataType:"json", data:JSON.stringify(param), //用于将 JavaScript 值转换为 JSON 字符串一定要用这个函数,否则会400 success:function(data){ console.log(data); if(data.code==0){ window.location.href="index.html";//页面跳转 } } }); });