U
U
Umid2016-11-20 18:19:02
Node.js
Umid, 2016-11-20 18:19:02

How to get message value sent via res.end(message) in ajax?

How to get the value for further processing and correct display in ajax
if the data was sent from node.js with res.end(...)?

In particular, this point is of interest:

var xhr = new XMLHttpRequest();
      xhr.open("GET", "/subscribe", true);

      xhr.onload = function() {
        var li = document.createElement('li');
        li.textContent = this.responseText;
        message.appendChild(li);

        subscribe();
      };

      xhr.onerror = xhr.onabort = function() {
        setTimeout(subscribe,300);
      };

      xhr.send('');


Here, for some reason, the res.end(message) passed from the node is not displayed on the screen.
Screenshot with an error:
3ae6b5715db5480db693e624aee38bd1.png


The question is settled. Found a way out.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2016-11-20
@DarCKoder

app.get('/getapi', function(req, res, next){
    let body = req.body;
    return res.json({data: body});
});

$.get("/getapi",
    {
        param1: "param1",
        param2: "param2"
    },
    onAjaxSuccess
);
function onAjaxSuccess(data){
    console.log(data);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question