Я пытаюсь получить что-то вроде 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">×</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.info("Connected!");
console.info("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.info(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.info(JSON.stringify(pathEnd));
console.info( uname );
console.info( password );
console.info( email );
/*
for (var i in pathArray) {
response.send(console.info(i));
}
*/
response.send(JSON.stringify("weee"));
}
интересно, может ли кто-нибудь помочь мне с этим, спасибо! И извините, я все еще новичок в этом.
Что именно вызывает этот код, это обработчик события нажатия кнопки?
да, функция должна срабатывать при нажатии определенной кнопки, что я и сделал, но это не работает
может быть, что оригинальный обработчик событий для формы вызывается раньше, чем ваш. Попробуйте что-нибудь сделать с этим stackoverflow.com/a/2629954/5714855



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Зачем добавлять пользовательский ввод в URL? Для регистрации это должен быть POST-запрос.