current position:Home>Calling setcookie in Ajax fails without any error in the IDE

Calling setcookie in Ajax fails without any error in the IDE

2022-04-29 05:18:20Moon in bubble

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" placeholder=" Please enter your user name " class="form-control required" name="username" id="username" tips=" Please fill in the user name " />
    <input type="password" placeholder=" Please input a password " class="form-control required" id="password" name="password" tips=" Please fill in the password " />
    <input type="text" name="cpacha" id="cpacha" maxlength="4" class="form-control required" placeholder=" Verification Code " tips=" Please fill in the verification code " >

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 src="admin/js/bootstrap.min.js"></script>
<script src="admin/js/util.js"></script>
<script type="application/javascript"></script>
<script type="text/javascript">
      var user={};
      user.username= $("#username").val();
      user.password =$("#password").val();
      user.cpacha = $("#cpacha").val();

          if(data.code == 0){
            alert("setCookie front ");
            alert("setCookie after ");
            window.location.href = 'index';

          alert(' Network error !');


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

setCookie No definition ,

So press Ctrl Click the mouse , Find access to util.js

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())
    var strCookie=window.document.cookie;
     alert("function setCookie in "+strCookie);

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


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 src="/admin/js/bootstrap.min.js"></script>
<script src="/admin/js/util.js"></script>
<script type="application/javascript"></script>

  Perfect operation !!!!

copyright notice
author[Moon in bubble],Please bring the original link to reprint, thank you.

Random recommended