我正在尝试将表单数据推送到MongoDB。我正在使用NodeJS作为Web服务器。我正在笔记本电脑上运行这两个程序。
下面是代码,并请注意确保需要更改哪个部分。数据库名称为“ research”,集合名称为“ biomaker”。
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.2.1.min.js"></script>
<script src="https://unpkg.com/vue"></script>
<script src="https://surveyjs.azureedge.net/1.0.48/survey.jquery.min.js"></script>
<div style="padding:50px">
<div id="app">
<h2>{{ message }}</h2>
</div>
<div id="surveyContainer"></div>
</div>
new Vue({
el: '#app',
data: {
message: 'Online Questionnaire'
}
})
Survey.Survey.cssType = "bootstrap";
var surveyJSON = {
pages: [{
name: "save_data_1",
elements: [{
type: "text",
name: "question3",
title: "First Name"
},
{
type: "text",
name: "question4",
title: "Last Name"
},
{
type: "text",
name: "question5",
title: "Date Of Birth"
},
{
type: "text",
name: "question6",
title: "Country"
}
]
}]
}
function sendDataToServer(survey) {
alert("The results are:" + JSON.stringify(survey.data) + ". The results can be sent to a API server and save to a database.");
var data = {
"request": "save_data_1",
sdata: survey.data
};
$.ajax({
headers: {},
type: "POST",
url: "127.0.0.1:27017/research.biomarker",
contentType: "application/json",
charset: "utf-8",
dataType: "json",
error: function(jqXHR, error, errorThrown) {
if (jqXHR.status) {
alert(jqXHR.responseText);
} else {
alert("Something went wrong");
}
},
data: JSON.stringify(data),
success: function(c, textStatus, request) {
},
})
}
var survey = new Survey.Model(surveyJSON);
$("#surveyContainer").Survey({
model: survey,
onComplete: sendDataToServer
});
body {
background-color: #dbecec;
}
答案 0 :(得分:0)
我设法弄清了用html构建表单,然后使用get将其推送到firebase或mongodb之类的数据库的必要条件