Javascript 使用Ajax、jquery、Node.js和Express发布数据
我需要一些帮助。我在互联网上找到了很多不同的东西,但我无法理解这个过程。 我有一个旋转木马,我的用户在注册后立即被重定向到。 我想通过问一些简单的问题来了解他们的个人资料。 这是帕格密码Javascript 使用Ajax、jquery、Node.js和Express发布数据,javascript,jquery,ajax,node.js,express,Javascript,Jquery,Ajax,Node.js,Express,我需要一些帮助。我在互联网上找到了很多不同的东西,但我无法理解这个过程。 我有一个旋转木马,我的用户在注册后立即被重定向到。 我想通过问一些简单的问题来了解他们的个人资料。 这是帕格密码 body div(class='container fill') div(id='myCarousel', class='carousel slide', data-ride='carousel', data-interval="false", data-wrap="false") div(clas
body
div(class='container fill')
div(id='myCarousel', class='carousel slide', data-ride='carousel', data-interval="false", data-wrap="false")
div(class='carousel-inner')
div(class='item active')
div(class='carousel-caption')
div(class='form-group', method='post')
h1(id='welcome') WELCOME
h1
| Tell us more about yourself
h2
| Pick any interest you like
label(for='shopping', class="radio-inline")
| Shopping
input(type='checkbox', id='round', name='interest_filter', value='2', checked=interest=='shopping')
div
label(for='travelling', class="radio-inline")
| Travelling
input(type='checkbox', id='round', name='interest_filter', value='3', checked=interest=='travelling')
div
label(for='musique', class="radio-inline")
| Musique
input(type='checkbox', id='round', name='interest_filter', value='4', checked=interest=='musique')
div
label(for='cooking', class="radio-inline")
| Cooking
input(type='checkbox', id='round', name='interest_filter', value='5', checked=interest=='cooking')
div
label(for='nature', class="radio-inline")
| Nature
input(type='checkbox', id='round', name='interest_filter', value='6', checked=interest=='nature')
div
label(for='arts', class="radio-inline")
| Arts
input(type='checkbox', id='round', name='interest_filter', value='7', checked=interest=='arts')
这就是我不知道如何给出数据的地方
$('#matchme').click(function(){
var data = {};
$.ajax({
url: '/carousel',
type: 'POST',
cache: false,
data: JSON.stringify(data),
success: function(data){
alert('post success')
console.log(JSON.stringify(data));
}
});
});
这在我的app.js中(我将使用瀑布来获取数据并将其存储在我的数据库中)
我使用输入是因为你不能把表格放在旋转木马里。
到目前为止,我理解这个概念,但我已经使用Javascript一个月了,我被卡住了,有什么帮助吗?
提前谢谢 首先,您必须收集来自用户的所有信息,并在Ajax调用中传递该对象。除了空白对象,我看不到您在请求期间传递的任何数据。之后,在服务器端,您可以获得您已经编写的
req.body
中的所有信息
$('#matchme').click(function(){
var data = {name:'hola', info: 'info'};
$.ajax({
url: '/carousel',
type: 'POST',
cache: false,
data: JSON.stringify(data),
success: function(data){
alert('post success')
console.log(JSON.stringify(data));
}
});
});
首先,您必须收集来自用户的所有信息,并在Ajax调用中传递该对象。除了空白对象,我看不到您在请求期间传递的任何数据。之后,在服务器端,您可以获得您已经编写的
req.body
中的所有信息
$('#matchme').click(function(){
var data = {name:'hola', info: 'info'};
$.ajax({
url: '/carousel',
type: 'POST',
cache: false,
data: JSON.stringify(data),
success: function(data){
alert('post success')
console.log(JSON.stringify(data));
}
});
});
首先,如果我正确理解了这个问题,我建议使用class属性而不是id来对每个元素应用“round”样式 如果您改变了这一点,您可以使用普通的JavaScript:
document.getElementById('someId').value
语法来提取不同的值
如果您使用的是JQuery库,则更容易:$(“#someId”).val()代码>
首先,如果我正确理解了问题,我建议使用class属性而不是id来对每个元素应用“round”样式
如果您改变了这一点,您可以使用普通的JavaScript:document.getElementById('someId').value
语法来提取不同的值
如果您使用的是JQuery库,则更容易:$(“#someId”).val()代码>
谢谢你的帮助,我现在明白了这个概念。
到目前为止,它一直致力于:
$(document).ready(function() {
console.log("document is ready");
$("#matchme").click(function(){
event.preventDefault();
var data = {};
data.sex = $('[name=sex]:checked').val();
data.orientation_filter = $('[name=orientation_filter]:checked').val();
data.age = $('[name=age]:checked').val();
console.log(data.sex);
console.log(data.orientation);
console.log(data.age);
$.ajax({
url: '/carousel',
type: 'POST',
cache: false,
data: JSON.stringify(data),
success: function(data){
alert("post success")
console.log(JSON.stringify(data));
}
});
});
});
所以我可以在记录时得到data.sex的值,但是其他两个data.orientation和data.age仍然没有定义
以下是我的app.js文件:
app.post('/carousel', function (req, res) {
username = session.uniqueID;
var data = {
sex: req.body.sex,
orientation: req.body.orientation_filter,
age: req.body.age
};
console.log(data.sex);
console.log(data.age);
function UserStart(data, callback) {
async.waterfall([
function(cb) {
pool.getConnection(function (err, connection) {
if (err) {
console.log(err);
}
connection.query('INSERT INTO usersinfo SET ?', data , function (err, rows, fields) {
if (err) {
console.log(err, "error")
} else {
callback(null, "all good")
}
});
});
}
],
function (err, data) {
if (err) {
callback(err);
} else {
callback(null, data);
}
})
}
UserStart(data, function (err, callback) {
if (err) {
res.redirect('/login');
console.log("login again");
} else {
res.redirect('/home');
console.log("user is now activated, ready !");
}
})
});
谢谢你的帮助,我现在明白了。
到目前为止,它一直致力于:
$(document).ready(function() {
console.log("document is ready");
$("#matchme").click(function(){
event.preventDefault();
var data = {};
data.sex = $('[name=sex]:checked').val();
data.orientation_filter = $('[name=orientation_filter]:checked').val();
data.age = $('[name=age]:checked').val();
console.log(data.sex);
console.log(data.orientation);
console.log(data.age);
$.ajax({
url: '/carousel',
type: 'POST',
cache: false,
data: JSON.stringify(data),
success: function(data){
alert("post success")
console.log(JSON.stringify(data));
}
});
});
});
所以我可以在记录时得到data.sex的值,但是其他两个data.orientation和data.age仍然没有定义
以下是我的app.js文件:
app.post('/carousel', function (req, res) {
username = session.uniqueID;
var data = {
sex: req.body.sex,
orientation: req.body.orientation_filter,
age: req.body.age
};
console.log(data.sex);
console.log(data.age);
function UserStart(data, callback) {
async.waterfall([
function(cb) {
pool.getConnection(function (err, connection) {
if (err) {
console.log(err);
}
connection.query('INSERT INTO usersinfo SET ?', data , function (err, rows, fields) {
if (err) {
console.log(err, "error")
} else {
callback(null, "all good")
}
});
});
}
],
function (err, data) {
if (err) {
callback(err);
} else {
callback(null, data);
}
})
}
UserStart(data, function (err, callback) {
if (err) {
res.redirect('/login');
console.log("login again");
} else {
res.redirect('/home');
console.log("user is now activated, ready !");
}
})
});
到底是什么问题?你在哪里被卡住了?req.body的值是什么?我对ajax部分很感兴趣,我不知道该怎么做,我如何才能获得我的用户在数组中标记为数据的所有值,然后通过app.post获得它?req.body的值是用户勾选的复选框,例如(烹饪、旅行等)使用表单并序列化formI不能在旋转木马中使用表单,我只能使用输入。我只想得到我的用户检查的值,这到底是什么问题,你在哪里被卡住了,req.body的值是什么?我对ajax部分很感兴趣,我不知道该怎么做,我如何才能得到我的用户在数组中勾选为数据的所有值,然后用app.post获得它?req.body的值是用户勾选的复选框,例如(烹饪、旅行等)使用表单并序列化formI不能在旋转木马中使用表单,我只能使用输入。我只想得到我的用户检查的值。谢谢你的回复,我也尝试过这种方法,但是值仍然没有定义,我不明白为什么。嗨,谢谢你的回复,我也尝试过这种方法,但是值仍然没有定义,我不明白为什么。