Serverside and clientside javascript

前提是你 提交于 2019-12-18 09:24:33

问题


Does serverside javascript exist, if yes, is it possible to clientside javascript to interact with serverside javascript?

For example, is it possible for clientside javascript to request from the serverside javascript to return part of a json file which is stored on the server without downloading the whole json file?

I understand that I can do this with other serverside languages like aspx, php etc etc, but wanted to know if serverside javascript exists and if it can do something similar with json files?


回答1:


Does serverside javascript exist

http://en.wikipedia.org/wiki/Comparison_of_server-side_JavaScript_solutions

Node.js is the current, popular way to do this.

if yes, is it possible to clientside javascript to interact with serverside javascript?

Yes. In the context of the WWW, this is usually done the same way that any other communication with server side programs is done: By making HTTP requests (usually via XMLHttpRequest).

For example, is it possible for clientside javascript to request from the serverside javascript to return part of a json file which is stored on the server without downloading the whole json file?

I understand that I can do this with other serverside languages like aspx, php etc etc, but wanted to know if serverside javascript exists and if it can do something similar with json files?

JavaScript is a programming language. It can do more or less anything on the server that any other language can do.




回答2:


Backend and frontend technologies almost always communicate via HTTP request/responses in general. This allows any pair of technologies to communicate in a language agnostic manner.




回答3:


Going on what Quentin said above

Server side JavaScript is completely possible yes, using Node.js will also offer you a much wider range than simple XMLHttpRequest's.

Using modules with NodeJs try Websocket.io allows you to talk between Client and Server very much more easily.




回答4:


Client side with jquery:

$(document).ready(function(){
  function fireAjax(){
    $.getJSON('/ajax/partialJSON', {desiredKey:'foo'}, function(data){
      console.log(data);
    });
  };
  $('#button').on('click', fireAjax)
};

Server side, using node.js and the Express web framework:

app.get('/ajax/partialJSON', function(req,res){
  var key = req.query['desiredKey'];
  // parse your JSON here
  var data = {foo:'Hello;', bar:'World'};
  var value = data[key] || null;
  res.json(value);
});


来源:https://stackoverflow.com/questions/14733848/serverside-and-clientside-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!