[英]Shared Desktop & Web based Javascript REST / Ajax
I am running a node.js back-end with a Web based javascript client to send AJAX requests. 我正在使用基于Web的javascript客户端运行node.js后端,以发送AJAX请求。
Now, I would like to do a small desktop version of the javascript client using pure Javascript, ie Google's V8. 现在,我想使用纯Javascript(即Google的V8)制作javascript客户端的小型桌面版本。
In this scenario, how could I enable the AJAX support for the desktop version as there are no window / document objects ? 在这种情况下,由于没有窗口/文档对象,如何启用对桌面版本的AJAX支持? In a perfect world in such a way that the Web and Desktop versions would share the same code ?
在一个完美的世界中,Web版本和Desktop版本将共享相同的代码?
Or would I need to write the AJAX or REST API outside the js code, ie as native code and call it from js ? 还是我需要在js代码之外编写AJAX或REST API,即作为本机代码并从js调用它?
As Brad, says, there is currently no way to make an http request from the ECMAScript non-web implementations, even though there is a working draft at http://www.w3.org/TR/XMLHttpRequest/ to integrate XMLHttpRequest to these implementations as well (I guess through external bindings). 正如Brad所说,即使在http://www.w3.org/TR/XMLHttpRequest/上有一个将XMLHttpRequest集成到这些应用程序中的工作草案,目前也无法从ECMAScript非Web实现中发出http请求。实现(我想通过外部绑定)。
What I did was to create a global function and implement it in the Web version like this: 我要做的是创建一个全局函数,并在Web版本中实现它,如下所示:
function SendBackendRequest( url, parameters, func )
{
var request=new ajaxRequest();
request.onreadystatechange=function() {
if (request.readyState == 4) {
if ( request.status == 200 || window.location.href.indexOf("http") ==-1 ) {
func.call( this, request.responseText );
} else {
func.call( this, "" );
}
}
}
request.open( "POST", "http://localhost" + url, true );
request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
request.send( parameters );
};
ajaxRequest=function()
{
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
if ( window.ActiveXObject ) {
// --- IE
for (var i=0; i<activexmodes.length; i++) {
try {
return new ActiveXObject(activexmodes[i])
}
catch(e) {
//suppress error
}
}
} else
{
if (window.XMLHttpRequest) // --- Mozilla, Safari etc
return new XMLHttpRequest();
else return false;
}
};
On the Desktop side I just implemented SendBackendRequest() as well and inside my client code can use the same code to communicate with the backend. 在桌面端,我也刚刚实现了SendBackendRequest(),并且我的客户端代码内部可以使用相同的代码与后端进行通信。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.