Send data to client without using an additional url

Asked

Viewed 250 times

2

Hello. What I want to know is the best way to send data to the client and this data will be received through a function (which can be executed in a click).

In the example below I will render a page (I am using express in Node.js):

app.get('/exemplo', function (req, res) {
  res.render('exemplo.html');
});

But if I want to send data to the rendered page, I will be required to use one url additional? As below:

app.get('/exemplo', function (req, res) {
  res.render('exemplo.html');
});
var data = {...}
app.get('/exemplo/data', function (req, res) {
  res.send(data);
});

Or you could do both using the same url? Or is there some other way to send data, for example user data to the page, using the expressJs.

Note: I do not use the pre-processor Jade.

From now on, thank you.

  • Can’t you just send a <script type...>var meuJson = { ... }<script> next to HTML? There’s probably a thousand simple ways to solve the problem, but I think the question is a little vague. Anyway, one thing is fact: qq URL that you provide pro JS from the client, the client can use as he wants to get the data that the JS would get. Another solution would be to concatenate JSON after HTML and split it into 2 separate strings later. You can only reply with more details.

  • @Bacco, look at my edition. See if it’s clearer.

1 answer

1


You can use the same route on the Express and send the data on query string, as a GET.

You can do this with ajax and then on Express .query, first argument (req) in the controller.

An example would be like this:

In Javascript, using ajax:

var data = {foo: 'bar'};
ajax(data, function(res) {
    console.log('Resposta do servidor:', res);
});

And on Node/Express, using the req.query:

app.get('/exemplo', function (req, res) {
  var txt = req.query.foo + ' bar';
  res.render({resposta: txt}); // vai mandar de volta a string para o JS
});
  • Sergio, how would be the use of queryString in an object coming from the server and being read in js?

  • @Samirbraga as so coming from the server? You mean sending data from the server back to ajax?

  • That. In your example, you sent the ajax object to the server and read it with the req.query. Where can I read, in js, this {resposta: text}?

  • @Samirbraga saw the link I put in "No Javascript, using ajax:"? this goes to a jsFiddle with a native JS ajax. If you use that, there’s an example res of the callback. As I wrote here in reply: console.log('Resposta do servidor:', res);. If you use jQuery is the same, it will be passed to the callback. I could explain to me?

  • Sergio, yeah I had seen Jsfiddle, I was just having trouble using the prepareData() along with the Angular, and I’m short on time to test it now, but I imagine I got it right. Thanks for the clarification.

  • @Samirbraga ah, because at the angle his ajax should already have this integrated. My example was with native JS. But this prepareData is only for when sending, to receive only the line is used callback(JSON.parse(request.responseText));, I imagine that the angular also has a way to respond with a JSON.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.