public <T> T yourMethodSignature(Class<T> type) {
// get some object and check the type match the given type
Object result = ...
if (type.isAssignableFrom(result.getClass())) {
return (T)result;
} else {
// handle the error
}
}
Итак, из комментариев я сделал это изменение в своем app.js
:
app.post('/', function(req, res) {
res.send(req.body.data);
});
Тогда в моем index.js
:
var xhr = new XMLHttpRequest();
xhr.open("POST", "/", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
data: data
}));
xhr.onreadystatechange = function(){
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
console.log(xhr.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
В принципе, я отсутствовал res.send()
изначально. И когда эта информация отправляется, я смог console.log
ответить на мой index.js
.
Вам не нужно добавлять, если условие Попробуйте это
app.post('/', function(req, res) { console.log(req.body.code); });