使用 JavaScript 进行 RPC(远程过程调用)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10006799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:27:52  来源:igfitidea点击:

Using JavaScript for RPC (Remote Procedure Calls)

phpjavascriptjsonxmlhttprequestrpc

提问by Asher

Whats the best way to do a RPC (Remote Procedure Call) from a webpage or from JavaScript code? I want to keep it in JavaScript if possible so that I can do live updates to the webpage without having to do anything in PHP in case my server goes down I still want the JavaScript to handle page updates... possibly even sending a request to a python shell script running locally... Is this legal from JavaScript?

从网页或 JavaScript 代码执行 RPC(远程过程调用)的最佳方法是什么?如果可能,我想将它保留在 JavaScript 中,以便我可以对网页进行实时更新,而无需在 PHP 中执行任何操作,以防万一我的服务器出现故障我仍然希望 JavaScript 处理页面更新......甚至可能向一个在本地运行的 python shell 脚本......这在 JavaScript 中合法吗?

I prefer having remote machines handling the requests. I see a lot of talk about how XMLRPC or JSONRPC can do this however, I haven't seen any good examples. I guess Microsoft suggests using their XMLhttprequest however, I haven't seen anything that doesn't use their ActiveX call or require special code for Internet Explorer... I just want some simple way of passing a command to some python/ruby/c++ code from a webpage.

我更喜欢让远程机器处理请求。我看到很多关于 XMLRPC 或 JSONRPC 如何做到这一点的讨论,但是,我还没有看到任何好的例子。我猜微软建议使用他们的 XMLhttprequest 但是,我还没有看到任何不使用他们的 ActiveX 调用或需要 Internet Explorer 的特殊代码的东西......我只是想要一些将命令传递给某些 python/ruby/c++ 的简单方法来自网页的代码。

Python Server Code (Waiting for a RPC Request):

Python 服务器代码(等待 RPC 请求):

import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer
def my_awesome_remote_function(str):
    return str + "awesome"
server = SimpleXMLRPCServer(("localhost", 8000))
print "Listening on port 8000..."
server.register_function(is_even, "is_even")
server.serve_forever()

EXAMPLE JavaScript Code:

示例 JavaScript 代码:

var client = rpc.server("http://localhost:8000/");
var my_local_variable = client.my_awesome_remote_function(param);

Is there a good JSON/JavaScript example someone can point me to that sends a request to a server and gets some data back from that server?

是否有一个很好的 JSON/JavaScript 示例,有人可以指出我向服务器发送请求并从该服务器获取一些数据?

Thanks!

谢谢!

采纳答案by kirilloid

Hardly it will work this way: client.my_awesome_remote_function. There's no magic in js like __callin php. Also remote calls are done in js mostly asynchronously using idea of callback - function which is called after finishing of some task.

它几乎不会以这种方式工作:client.my_awesome_remote_function. js__call中没有像php那样的魔法。此外,在 js 中远程调用主要是使用回调的思想异步完成的 - 在完成某些任务后调用的函数。

var client = rpc.server("http://localhost:8000/");
var my_local_variable;
client.rpc('my_awesome_remote_function', [param], function(result) {
    my_local_variable = result;
});

You can easily find tutorials about that calls. Just google "ajax tutorials". E.g.: http://www.w3schools.com/ajax/ajax_intro.asp(event though w3schools isn't the best site and have errors in some details, it is still good for beginners).

您可以轻松找到有关该调用的教程。只需谷歌“ajax 教程”。例如:http: //www.w3schools.com/ajax/ajax_intro.asp(事件虽然 w3schools 不是最好的网站并且在一些细节上有错误,但它仍然适合初学者)。

All ajax implementations use both modern both XMLHttpRequest and ActiveX control for older IE.

所有 ajax 实现都为旧版 IE 使用现代 XMLHttpRequest 和 ActiveX 控件。

It is possible to run those requests synchronously, but is considered very bad from the point of user experience. Also, you'll need to deal with concept of callbacks anyway.

可以同步运行这些请求,但从用户体验的角度来看,这被认为是非常糟糕的。此外,无论如何您都需要处理回调的概念。