Как обрабатывать отправленные данные на сервер в экспресс?

   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
        }
   }
3
задан catandmouse 14 July 2018 в 01:09
поделиться

2 ответа

Итак, из комментариев я сделал это изменение в своем 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.

0
ответ дан catandmouse 17 August 2018 в 12:04
поделиться

Вам не нужно добавлять, если условие Попробуйте это

app.post('/', function(req, res) { console.log(req.body.code);  });
0
ответ дан Abhi Anand 17 August 2018 в 12:04
поделиться
Другие вопросы по тегам:

Похожие вопросы: