Last active
January 18, 2016 00:09
-
-
Save sj82516/e62436f6e2dc6d320311 to your computer and use it in GitHub Desktop.
NodeJs-form/Ajax/Handle request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
form#formArticle(role="form") | |
div.form-group | |
label(for="createArticleTitle") Article Title | |
textarea.form-control(row="1" name="title") | |
label(for="createArticleBody") Article Body | |
textarea.form-control(row="6" name="description") | |
button(type="submit") | |
//使用jquery的serialize()-直接將form各個欄位值轉為POST/GET傳遞的格式 | |
//Use Jquery serialize() function - turn the values of cloumns in form into POST/GET format | |
//*項目必須要有 name | |
//*column should set name attr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- PUT/POST/GET --> | |
//PUT | |
$("#formDescription").submit(function(){ | |
event.preventDefault(); | |
console.log(account); | |
$.ajax({ | |
type: "PUT", | |
url: "http://127.0.0.1:3000/users/"+ account +"/description", | |
data: $(this).serialize(), | |
success: function(data){ | |
console.log(data); | |
}, | |
error: function(data){ | |
console.log(data); | |
} | |
}); | |
}); | |
//POST | |
$("#formArticle").submit(function(){ | |
event.preventDefault(); | |
console.log(account); | |
$.ajax({ | |
type: "POST", | |
url: "http://127.0.0.1:3000/users/"+ account +"/article", | |
data: $(this).serialize(), | |
success: function(data){ | |
console.log(data); | |
}, | |
error: function(data){ | |
console.log(data); | |
} | |
}); | |
}); | |
//GET | |
$.ajax({ | |
type: "GET", | |
url: "http://127.0.0.1:3000/signup", | |
data: { "account":tmpAccount}, //-> it will auto transfer into http://url/?account=tmpAccouont | |
success: function(data){ | |
console.log(data); | |
}, | |
error: function(data){ | |
console.log(data); | |
} | |
}); | |
//DELETE | |
$(".deleteArticle").on('click', function(){ | |
var artilce = $(this).closest(".article"); | |
var articleID = $(artilce).closest(".article").attr("id"); | |
$.ajax({ | |
type: "DELETE", | |
url: "http://127.0.0.1:3000/users/"+ account +"/article", | |
data: {articleID: articleID}, | |
success: function(data){ | |
console.log(data); | |
$(artilce).remove(); | |
}, | |
error: function(data){ | |
console.log(data); | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//GET/PUT use req.query.* -> to get passing data | |
//POST/DELETE use req.body.* -> to get passing data | |
router.route('/:account/article'). | |
get(function(req, res, next) { | |
Article.find({author: req.user.account}, function(err, articles){ | |
if(err){ | |
console.log(err); | |
res.status(401).json("get articles error"); | |
return; | |
} | |
console.log(articles); | |
res.status(201).json(articles); | |
}); | |
}) | |
.post(function(req, res, next){ | |
var articleData = req.body; | |
var article = new Article({ | |
author: req.user.account, | |
title: articleData.title, | |
description: { body: articleData.description, time: Date.now()}, | |
createTime: Date.now() | |
}); | |
article.save(function(err, article){ | |
if(err){ | |
console.log(err); | |
res.status(401).json("error"); | |
return; | |
} | |
console.log(article); | |
res.status(201).json("sucess"); | |
return; | |
}); | |
}).put(function(req, res, next){ | |
Article.findOne({_id: req.params.articleID}, function(err, article){ | |
article.description.body = req.body.description; | |
article.description.time = Date.now(); | |
}) | |
}).delete(function(req, res, next){ | |
console.log("delete"); | |
Article.remove({_id: req.body.articleID}, function(err, removed){ | |
if(err){ | |
console.log(err); | |
res.status(401).json("sucess"); | |
return; | |
} | |
console.log(removed); | |
res.status(201).json("sucess"); | |
return; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment