Created
December 26, 2011 03:34
-
Star
(122)
You must be signed in to star a gist -
Fork
(37)
You must be signed in to fork a gist
-
-
Save diorahman/1520485 to your computer and use it in GitHub Desktop.
Ajax, call jQuery POST to node.js expressjs
This file contains hidden or 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
<html> | |
<head> | |
<title>jsonp test</title> | |
<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script> | |
<script type="text/javascript"> | |
$(function(){ | |
$('#select_link').click(function(e){ | |
e.preventDefault(); | |
console.log('select_link clicked'); | |
/*$.ajax({ | |
dataType: 'jsonp', | |
data: "data=yeah", | |
jsonp: 'callback', | |
url: 'http://localhost:3000/endpoint?callback=?', | |
success: function(data) { | |
console.log('success'); | |
console.log(JSON.stringify(data)); | |
} | |
});*/ | |
var data = {}; | |
data.title = "title"; | |
data.message = "message"; | |
$.ajax({ | |
type: 'POST', | |
data: JSON.stringify(data), | |
contentType: 'application/json', | |
url: 'http://localhost:3000/endpoint', | |
success: function(data) { | |
console.log('success'); | |
console.log(JSON.stringify(data)); | |
} | |
}); | |
/*$.ajax('http://localhost:3000/endpoint', { | |
type: 'POST', | |
data: JSON.stringify(data), | |
contentType: 'application/json', | |
success: function() { console.log('success');}, | |
error : function() { console.log('error');} | |
});*/ | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="select_div"><a href="#" id="select_link">Test</a></div> | |
</body> | |
</html> |
This file contains hidden or 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
var express = require('express'); | |
var app = express.createServer(); | |
app.use(express.bodyParser()); | |
/*app.get('/endpoint', function(req, res){ | |
var obj = {}; | |
obj.title = 'title'; | |
obj.data = 'data'; | |
console.log('params: ' + JSON.stringify(req.params)); | |
console.log('body: ' + JSON.stringify(req.body)); | |
console.log('query: ' + JSON.stringify(req.query)); | |
res.header('Content-type','application/json'); | |
res.header('Charset','utf8'); | |
res.send(req.query.callback + '('+ JSON.stringify(obj) + ');'); | |
});*/ | |
app.post('/endpoint', function(req, res){ | |
var obj = {}; | |
console.log('body: ' + JSON.stringify(req.body)); | |
res.send(req.body); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//100% work
``//view data
$(document).on('click','.view',function(e){
e.preventDefault();
let id=$(this).attr('data')
$('#myModal').modal('show');
$.ajax({
type: "PUT",
url: "/updatemeeting/"+id,
dataType: "json",
data: JSON.stringify({id:id}),
contentType: 'application/json',
cache: false,
timeout: 5000,
complete: function() {
console.log('process complete');
},
dataType: "json",
success: function (response,status, xhr) {
var html = "";
html=html+"ID"+response._id+"";
html=html+"MetingName"+response.meetingName+"";
html=html+"Description"+response.description+"";
html=html+"StartTime"+response.startTime+" PM
";
html=html+"EndTime"+response.endTime+" PM
";
html=html+"Date"+moment(response.createDate).format('ddd Do MMM')+"";
$("#tableview").html(html);
},
error: function(xhr,status,error) {
console.log('process error',error);
},
});
})
//controller file
async AjaxView(req,res){
var id = req.params.id;
console.log("id=>",id);
if(!id){
req.flash('error','Something error')
}
const data=await Meeting.findOne({_id:id,moment:moment},(err,response)=>{
res.json(response);
})
console.log(data)
}
//route
app.put('/updatemeeting/:id', auth, MeetingController().AjaxView)