Calling setcookie in Ajax fails without any error in the IDE

2022-04-29 05:18:20

Recent projects , It took a long time because of this small mistake , I wish to record

Front end part code ：

Simplify the code in the form into

<form id="login-form" method="post">
<input type="text" name="cpacha" id="cpacha" maxlength="4" class="form-control required" placeholder=" Verification Code " tips=" Please fill in the verification code " >
</form>

Want to use ajax Set when judging whether the user name exists Cookie,js The code is as follows

<script src="admin/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script type="application/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){$("#submit-btn").click(function(){
return;
}
var user={};
user.username= $("#username").val(); user.password =$("#password").val();
user.cpacha = $("#cpacha").val();$.ajax({
type:'POST',
data:user,
dataType:'json',
success:function(data){
if(data.code == 0){
window.location.href = 'index';

}else{
showErrorMsg(data.msg);
}
},
error:function(data){
}
});
});
});

</script>

setCookie() Method in util.js in , Cannot be set while executable cookie Value , And cause login The page can't jump , But the background can match successfully .

The result is shown in Fig. ：

IDE No mistakes

But when I open the browser , Press F12 Get into Console , Found some clues

login Page js All the documents are red , There is a line , It also aroused my vigilance .

Uncaught ReferenceError: setCookie is not defined

function setCookie(name, value, days) {
var d = new Date;
d.setTime(d.getTime() + 24*60*60*1000*days);
window.document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
//console.log(name + "=" + value + ";path=/;expires=" + d.toGMTString())
}

setCookie Methods are also defined . Just when I can't figure it out , It suddenly occurred to me if javaScript Is there an execution order in the page , So the

<script>XXX</script>

Put it in the front

But it's still the problem .

When I was about to collapse , A flash of light , Think carefully , Is it right? Error in path reference

View folder hierarchy

hold <script></script> Inside src Add “/"

namely ：

<script src="/admin/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script type="application/javascript"></script>