简体   繁体   English

我们可以从WebService调用javascript函数吗?

[英]Can we call a javascript function from WebService?

I have a situation where I have to call aa javascript method from SQL Server. 我有一种情况,我必须从SQL Server调用一个javascript方法。 I haven't found any solution to do the same. 我没有找到任何解决方案来做同样的事情。 I found one thing that I can call web service from SQL Server but now the problem is "Can we call javascript function from web service (like we can do in code behind file of aspx page)"? 我发现有一件事我可以从SQL Server调用Web服务,但现在的问题是“我们可以从Web服务调用javascript函数(就像我们可以在aspx页面的文件后面编写代码)”吗?

Please help 请帮忙

Well, it depends on what you mean. 嗯,这取决于你的意思。 If I'm not mistaken the guys behind the free real-time communication platform, xSockets have been able to use a compiled stored procedure in SQL Server to use xSockets to dispatch a call to connected clients (eg using WebSockets). 如果我没有弄错自由实时通信平台背后的人, xSockets已经能够使用SQL Server中的编译存储过程来使用xSockets分派对连接客户端的调用(例如使用WebSockets)。 I'm not saying this is something you should do , I'm saying "I think they have done it". 我不是说这是你应该做的事 ,我说“我认为他们已经做到了”。 You will find contact info on their page or just tweet with #xsockets hash on Twitter and they will respond. 您可以在他们的页面上找到联系信息,或者只是在推特上使用#xsockets哈希推文,他们会回应。

You could do this using Server-Sent Events , which are supported in most recent browsers. 您可以使用最近浏览器支持的服务器发送事件来执行此操作。

The PHP: PHP:

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

echo "id: " . time() . " " . PHP_EOL;
echo "data: $message" . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();

The JS: JS:

var source = new EventSource('/php');
source.onmessage = function(e) {
    console.log(e.data);
};

You can "call" javascript directly from the webservice... Or more correctly spoken you can use a publish/subscribe pattern to publish a message from you webservice and subscribe to it in your javascript. 您可以直接从Web服务“调用”javascript ...或者更正确地说,您可以使用发布/订阅模式从您的webservice发布消息并在您的javascript中订阅它。

Like @Daniel says we (XSockets) have called javascript from a compiled stored procedure, but that was a few years ago... I think it will be easier for you to take the approach where you "call" the javascript from a webservice. 就像@Daniel说的那样,我们(XSockets)已经从编译的存储过程调用了javascript,但那是几年前...我认为你更容易采用你从web服务“调用”javascript的方法。

To do it between webservice and javascript could be something like this... JAVASCRIPT 在webservice和javascript之间做这件事可能是这样的...... JAVASCRIPT

//Setup a subscription for the event 'foo'
conn.on('foo', function(data){
    console.log('foo - some data arrived from the webservice', data);
});

C# (or VB if you want to) C#(或VB,如果你想)

//Connect to the realtime server and the default controller generic
var conn = ClientPool.GetInstance("ws://127.0.0.1:4502/Generic", "*");
//We send an anonymous message here, but any object will work.
conn.Send(new { Message = "Hello JavaScript from C#" },"foo");

Full example here 完整的例子在这里

There may be some easier way to do this as well... if I knew a little bit more about what you are doing :) 也许有一些更简单的方法可以做到这一点......如果我对你正在做的事情了解得多一点:)

不,您不能从服务器上的Web服务调用客户端上的javascript函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们可以使用JavaScript Function从JSP调用Servlet吗? - Can we call Servlet from JSP using JavaScript Function? 我们如何从代码文件中调用javascript函数? - How can we call javascript function from code file? 我们如何直接从kml文件中调用javascript函数 - How can we call directly a javascript function from a kml file 从javascript调用网络服务 - Call a webservice from javascript 我们可以从另一个html调用另一个内部javascript函数来调用内部javascript函数吗? - can we call the internal javascript function from another internal javascript function from another html? 我们可以在 react js 中调用 javascript function 吗? - Can we call javascript function in react js? 在原生 safari 应用程序扩展中,我们如何从 swift 调用带有参数的 javascript 函数? - In Native safari app extension How we can call a javascript function with argumemts from swift? 我们可以从 JavaScript Post 方法调用 aspx.cs 中的非静态函数吗 - Can we call non-static function in aspx.cs from JavaScript Post Method 我们可以从javascript调用带有其他函数名称作为方法参数的本机方法吗? - can we call a native method from javascript with another function name as a parameter to the method? 我们如何从asp.net中的代码隐藏调用javascript函数? - How we can call javascript function from code-behind in asp.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM