[英]Why does my browser loading JSON response from Express?
I have a website with a form POST action. 我有一个带有表单POST操作的网站。 I want users to input their email into the form.
我希望用户将其电子邮件输入表单。 Once and email is submitted, a POST is sent to the server.
电子邮件提交后,POST将发送到服务器。 Using Express and the MailChimp API I save this email to my MailChimp account with the backend app.js file.
使用Express和MailChimp API,我将此电子邮件与后端app.js文件保存到我的MailChimp帐户中。
After successfully adding the email, I send a dummy JSON object using res.json(). 成功添加电子邮件后,我使用res.json()发送一个虚拟JSON对象。 For now it is just a dummy JSON object, but in the future I would use it to send success details.
目前,它只是一个虚拟JSON对象,但是将来我会用它发送成功详细信息。 The problem is my browser redirects from my index.html to display this JSON file.
问题是我的浏览器从我的index.html重定向到显示此JSON文件。 I don't want the page to reload, or redirect to the JSON response.
我不希望页面重新加载或重定向到JSON响应。 What am I doing wrong?
我究竟做错了什么?
would really love any help I could get, thanks :) 真的很喜欢我能得到的任何帮助,谢谢:)
I have tried: res.end() and res.send() and res.sendStatus(200) 我试过了:res.end()和res.send()和res.sendStatus(200)
APP.JS (just POST stuff): APP.JS(只是POST内容):
// Signup Route
app.post('/signup', (req, res) => {
const {email} = req.body;
console.log(req);
// Make sure fields are fillled
if(!email){
res.redirect('/fail.html');
return;
}
// Make request data
const data = {
members: [
{
email_address: email,
status: 'subscribed',
merge_fields: {} ///may need to remove this line....
}
]
};
const postData = JSON.stringify(data);
const options = {
url: 'myprivateurl',
method: 'POST',
headers: {
Authorization: 'myprivatekey'
},
body: postData
};
request(options, (err, response, body) => {
if(err) {
res.redirect('/fail.html');
}
else{
if(response.statusCode == 200){
//res.redirect('/success.html');
console.log("server side success");
res.json({
success: true,
message: "Some success message",
data: "some data if there's any"
});
}
else{
res.redirect('/fail.html');
}
}
});
});
HTML (just form stuff): HTML(仅表单形式):
<!--Email List-->
<div id="mailListHolder">
SIGN UP FOR OUR MAIL LIST!
<br>
<div id="successtext"> Thanks! </div>
<form id="emailform" action="/signup" method="POST">
<input name="email" type="text" placeholder="dopedude@mail.com" onfocus="this.placeholder = ''"/>
<input type="submit" />
</form>
</div>
CLIENT JS (just form stuff): CLIENT JS(仅用于表单):
$('#emailform').submit(function() {
console.log("emailform submit case WORKING!");
$("#emailform").css({
"display": "none",
});
$("#successtext").css({
"display": "block"
});
return false;
});
app.post('/api/user',(req,res)=>{ const user = new User({ email: req.body.email, password: req.body.password }); user.save((err,doc)=>{ if(err) res.status(400).send(err) res.status(200).send(doc) }) })
const userSchema = mongoose.Schema({ email:{ type:String, required:true, trim:true, unique:1 }, password:{ type:String, required:true, minlength:6 } })
const User = mongoose.model('User', userSchema )
or you can refer: https://github.com/karenaprakash/nodejs-sec-and-auth.git 或者您可以参考: https : //github.com/karenaprakash/nodejs-sec-and-auth.git
this project is demo for express.js 该项目是express.js的演示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.