当我调用index.js时,它在我的webhost服务器和cdn中也不工作,但在我的本地xampp服务器上工作得很好。

    <?php
    /* Main page with two forms: sign up and log in */
    require 'db.php';
    session_start();
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <title>Sign-Up/Login Form</title>
    <?php include 'css/css.html'; ?>
    </head>

    <?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
    if (isset($_POST['login'])) { //user logging in

        require 'login.php';

    }

    elseif (isset($_POST['register'])) { //user registering

        require 'register.php';

    }
    }
    ?>
    <body>
    <div class="form">

      <ul class="tab-group">
        <li class="tab"><a href="#signup">Sign Up</a></li>
        <li class="tab active"><a href="#login">Log in</a></li>
      </ul>

      <div class="tab-content">

         <div id="login">
          <h1>Welcome to </h1>

          <form action="index.php" method="post" autocomplete="off">

            <div class="field-wrap">
            <label>
              Email Address<span class="req">*</span>
            </label>
            <input type="email" required autocomplete="off"
 name="email"/>
          </div>

          <div class="field-wrap">
            <label>
              Password<span class="req">*</span>
            </label>
            <input type="password" required autocomplete="off"
    name="password"/>
          </div>

          <p class="forgot"><a href="forgot.php">Forgot Password?</a></p>

          <button class="button button-block" name="login" />Log
In</button>

          </form>

        </div>

        <div id="signup">
          <h1>Sign Up for Free</h1>

          <form action="index.php" method="post" id="form1"
    autocomplete="off">

          <div class="top-row">
            <div class="field-wrap">
              <label>
                First Name<span class="req">*</span>
              </label>
              <input type="text" required autocomplete="off"
 name='firstname' id="txtNumeric" />
             </div>

            <div class="field-wrap">
              <label>
                Last Name<span class="req">*</span>
              </label>
              <input type="text"required autocomplete="off"
 name='lastname'
    id="txtNumeric" />
            </div>
          </div>


          <div class="field-wrap">
            <label>
              Contact number<span class="req">*</span>
            </label>
            <input type="number"required autocomplete="off"
 name='contact'
    onclick="return AllowSingleSpaceNotInFirstAndLast();" />
          </div>


          <div class="field-wrap">
            <label>
              Email Address<span class="req">*</span>
            </label>
            <input type="email"required autocomplete="off" name='email'
    onclick="return AllowSingleSpaceNotInFirstAndLast();" />
          </div>

          <div class="field-wrap">
            <label>
              Set A Password<span class="req">*</span>
            </label>
            <input type="password"required autocomplete="off"
    name='password'/>
          </div>

          <button type="submit" class="button button-block"
  name="register"
    onclick="return validates()" />Register</button>

          </form>

        </div>

      </div><!-- tab-content -->

    </div> <!-- /form -->
    <script

 src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'>
    </script>

    <script src="js/index.js"></script>

    <script type="text/javascript">

    function validates(){

    $(function() {

    $('#txtNumeric').keydown(function (e) {

    if (e.shiftKey || e.ctrlKey || e.altKey) {

      e.preventDefault();

    } else {

      var key = e.keyCode;

      if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key
 <=
    40) || (key >= 65 && key <= 90))) {

        e.preventDefault();

      }

      }

     });

    });

    function AllowSingleSpaceNotInFirstAndLast() {
        var obj = document.getElementById('TextBox1');
        obj.value = obj.value.replace(/^\s+|\s+$/g, "");
        var CharArray = obj.value.split(" ");
        if (CharArray.length > 2) {
            alert("User name NOT VALID");
            return false;
        }[enter image description here][1]
        else {
            alert("User name VALID");
        }
        return true;
    }

    }

    </script>

</body>
</html>

这里的问题是我无法在我的web主机中正确运行代码。我的代码在本地xampp服务器上运行良好。我还编辑了数据库用户、数据库名称等,但没有帮助。有人能帮我吗?
谢谢您!
另外,我是新来的stackoverflow。对不起,我的格式不好

最佳答案

我认为问题出在你的文件路径你应该检查你的文件路径
或者可能是您没有在服务器上上载所有带有正确代码的文件。
替换此代码

<script src="js/index.js"></script>


<script src="<?php echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>/js/index.js"></script>

关于javascript - JS文件无法在Webhost中运行,但可以在我的本地Xampp上运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50712756/

10-12 00:07
查看更多