我找不到特定的网址

时间:2019-03-21 16:47:31

标签: javascript ajax server client

我正在尝试获取类似localhost:8080 / register / something / something / something

,但它仍像通常一样提供url: http://localhost:8080/?uname=Sam&email=12&password=12

我的功能是这样的

function register(){
    var uname1 = document.getElementById("uname").value;
    var password1 = document.getElementById("psw").value;
    var email1 = document.getElementById("email").value;

    if((uname !== "")&&(password !== "")&&(email !== "")) {
        var xhttp = new XMLHttpRequest();
        xhttp.open("GET", "/register/" + uname1 + "/" + password1 + "/" + email1, true);
        xhttp.send();
    }
}

HTML:

<div id="id02" class="modal">
        <form class="modal-content animate">
            <span onclick="document.getElementById('id02').style.display='none'" class="close" title="Close Modal">&times;</span>     <div class="container2">
                <label for ="uname"><b>Username</b></label>
                <input type="text" placeholder="Enter Username" name="uname" id="uname" required>

                <label for="psw"><b>Email</b></label>
                <input type="text" placeholder="Enter Email" name="email" id="email" required>

                <label for="password"><b>Password</b></label>
                <input type="password" placeholder="Enter Password" name="password" id="psw" required>

                <button class="button2" type="submit" onclick="register();">Register</button>
                <button type="button" onclick="document.getElementById('id02').style.display='none'" class="cancelbtn">Cancel</button>
            </div>
        </form>
    </div>

还有我的服务器:

//Import the express and url modules
var express = require('express');
//var url = require('url');

//The express module is a function. When it is executed it returns an app object
var app = express();

app.get('/register/*', regF);


app.use(express.static('website'));

//Start the app listening on port 8080
app.listen(8080);

var uname="";
var password="";
var email="";


//Function that adds test data to database4
//addData("name2","wads" ,"den@3");
function addData(n , p , e  ){

//Import the mysql module
    var mysql = require('mysql');

//Create a connection object with the user details
    var con = mysql.createConnection({
        host: "localhost",
        user: "gus",
        password: "12",
        database: "website"
    });



//Connect to the database
    con.connect(
        //This function is called when the connection is attempted
        function(err) {
            if (err) throw err;//Check for errors

            //Output results
            console.log("Connected!");
            console.log("YEY");

        }
    );

//----------------------------------------------------------------

    //Build SQL query
    var sql = "INSERT INTO customers (name, password, email) " +
        "       VALUES ( '"+  n +  "' , '"+ p +  "'  , '" + e + "' )";

    //Execute the query
    con.query(sql, function (err, result) {

        //Check for errors
        if (err) throw err;

        //Output results
        console.log(result.affectedRows + ' rows updated. ID is ' + result.insertId);

    });
    con.end();
}



function regF(request, response){

    //Split the path of the request into its components
    var pathArray = request.url.split("/");
     email = pathArray[pathArray.length - 1];
     password = pathArray[pathArray.length - 2];
     uname = pathArray[pathArray.length - 3];

    if(uname != "" && password!="" && email!=""){
        // addData("name3","wads" ,"den@3");
        addData( uname , password , email );
    }
    //console.log(JSON.stringify(pathEnd));
    console.log( uname );
    console.log( password );
    console.log( email );


    /*
     for (var i in pathArray) {

         response.send(console.log(i));

     }
 */

    response.send(JSON.stringify("weee"));
}

想知道是否有人可以帮助我,谢谢!抱歉,我还是这个新手。

0 个答案:

没有答案