为什么这个javascript代码不起作用

时间:2016-08-21 09:58:47

标签: javascript vert.x

有两个代码。一个工作,另一个工作不起作用。

我调试了它,我发现了

“var eb = new EventBus(”http://192.168.0.27:8081/bridge“);”

login.js中的这一行不能正常工作。

每次都非常感谢你。

工作

<html>
<head>
<title></title>
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="//cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js"></script>
<script src="vertx-eventbus.js"></script>
</head>

<style>
.news {
    font-size: 20pt;
}
</style>

<body>

    <div class="news">Latest news:</div>
    <br>

    <div id="status" class="news"></div>

    <script>
    var eb = new EventBus("http://192.168.0.27:8081/bridge");
    eb.onopen = function() {
        // set a handler to receive a message
        eb.registerHandler('call', function(error, message) {
            console.log('received a message: ' + JSON.stringify(message));
        });

        // send a message
        eb.send('call', {name : 'tim', age : 587});
    }
</script>

</body>
</html>

无法正常工作 - login.html

<!DOCTYPE html>
<!--
    login.html 
  -->
<html>
<head>
<meta charset="UTF-8">
<title>Flat HTML5/CSS3 Login Form</title>
</head>

<body>
    <div class="login-page">
        <div class="form">
            <form class="login-form" id="loginForm">
                <input type="text" id="username" placeholder="username" />
                <input type="password" id="password" placeholder="password" />
                <input class="submit" type="submit" title="Login" value="Login" onclick="check(this.form)" />

                <p class="message">
                    Not registered? <a href="#">Create an account</a>
                </p>
            </form>
        </div>
    </div>
    <script src="js/login.js"></script>
</body>
</html>

login.js

document.write("<script src='https://code.jquery.com/jquery-1.11.2.min.js'></script>");
document.write("<script src='//cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js'></script>");
document.write("<script src='/vertx-eventbus.js'></script>");   

function check(form)/*function to check userid & password*/
{
    /*the following code checkes whether the entered userid and password are matching*/
    if(form.username.value != "" && form.password.value != "") {
        var eb = new EventBus("http://192.168.0.27:8081/bridge");

        eb.onopen = function() {
            // set a handler to receive a message
            eb.registerHandler('call', function(error, message) {
                console.log('received a message: ' + JSON.stringify(message));
            });

            // send a message
            eb.send('call', {name : 'tim', age : 587});
        }

    } else {
        alert("Input Password or Username")/*displays error message*/
    }
}

1 个答案:

答案 0 :(得分:0)

你写过

document.write("<script src='/vertx-eventbus.js'></script>");   

与工作代码不同。还有一个额外的&#34; /&#34;之前&#34; vertx ..&#34;。这可能是问题吗?