【问题标题】:ASP.net web service using javascript to send commandsASP.net web 服务使用 javascript 发送命令
【发布时间】:2012-07-23 15:44:33
【问题描述】:

我希望通过 WebOS 平板电脑上使用的 javascript 向本地网络上的 Web 服务页面发送命令。问题是我以前从未这样做过,并且想知道是否有可能一开始就这样做? 我可以创建一个 VB.net 来收听网络服务调用/网页吗?

我将使用 Dreamweaver 从 phonegap 创建“应用程序”。我正在寻找使用 javascript 将字符串发送到 Web 服务的示例,我可以在 PC 上不断读取该服务,以便根据我在应用程序上按下的按钮根据需要执行任务。

举个例子:

 WebOS app > 
 button pushed in app to call up Internet Explorer > 
 sends "IE" string to WS > 
 WS triggers the correct exec to start depending on the string sent to it (IE) > 
 WS sends back "OK" to WebOS app when program is launched

有人有类似的例子吗?

更新 所以我会在我的javascript中做类似的事情?:

<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">

function buttonClicked(str what2Pass)
{
  $.ajax({
    async : false, /* set as true if you want asynchronous behavior */
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "http://localhost:7777/WebService.asmx/PerformAction",
    data: { "webOSCommand" : what2Pass},
    dataType: "json",
    success: function(data){
        alert(true);
    }
  });
}

<html>
<body>
<input type="button" id="button1" value="run IE on PC" onClick="buttonClicked('IE');" />
</body>
</html>

对于我的 WS 来说是:

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Diagnostics
Imports System.Web.Script.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<ScriptService()> _
Public Class Service
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function PerformAction(byRef webOSCommand as String) As String
        Dim oPro As New Process

        With oPro
            .StartInfo.UseShellExecute = True
            .StartInfo.Arguments = "http://www.google.com"
            .StartInfo.FileName = "internet explorer.exe"
            .Start()
        End With

        Return "Started"
    End Function

End Class

【问题讨论】:

    标签: javascript asp.net vb.net web-services


    【解决方案1】:

    是的,您可以通过 ajax 从 Javascript 调用 Web 服务。一个例子是这样的:

    $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "WebService.asmx/PerformAction",
      data: { "somestring" : "IE"},
      dataType: "json"
    });
    

    您的网络服务:

    [WebMethod]
     //parameter name must match the parameter name passed in from the Ajax call (sometring)
     public static string PerformAction(string somestring) 
     {
       return "something";
     }
    

    Detailed example here.

    【讨论】:

    • 我已经更新了我的 OP。我将如何在 PC 端检查通过 vb.net 发送的新值以启动“IE”?
    • @StealthRT 我不明白你的问题。 Client向WS发送what2Pass; WS 返回something。你想知道什么?
    • @lcarus:为了让我在后台执行一些自定义操作(打开中继端口、启动 .exe 等),我需要在 VB.net 中执行此操作。如何设置我的 VB.net 程序来监视 Web 服务并监听正在发送的那些命令?
    • 或者我可以在 VB.net 的 Web 服务页面本身上做我目前能够做的所有事情吗?
    • 我现在明白了。您无法监听发送到 WS 的命令。为什么没有 WS publish 将命令发送到某种消息队列,然后让 VB.net 程序从该队列中读取?一个简单的例子:WS 接收命令;在验证后插入数据库表等。VB.net 不断监视数据库表中插入表中的新记录并执行它需要的任何操作。 VB.net 可以在计时器或其他东西上,不断地轮询新记录。
    【解决方案2】:

    你所做的是正确的,但是你需要用 [ScriptService] 属性来装饰你的 webservice 类。

    [ScriptService]
      public class MyWebService : WebService {
       [WebMethod]
       public void MyMethod() {
       }
    }
    

    完成此操作后,您可以像以前一样通过 $.ajax 访问 Web 服务。

    更新

    对您的 javascript 函数稍作改动。我没有使用“完成”方法,而是设置传递给 $.ajax 方法的设置对象的“成功”属性。如果您希望 webservice 调用是异步的,您可以设置 async 属性,如下面的代码所示。

    function buttonClicked(str what2Pass)
    {
      $.ajax({
        async : false, /* set as true if you want asynchronous behavior */
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "http://localhost:7777/WebService.asmx/PerformAction",
        data: { "webOSCommand" : what2Pass},
       dataType: "json",
       success: function(data){
        alert(data);
       }
       });
    }
    

    【讨论】:

    • 感谢您添加该代码。我已经更新了 OP 以反映这一点。
    • 我现在无法对其进行测试,但请相信我,如果它有效或无效,请务必将其发布给其他人知道!
    • 谢谢。我也对刚才的javascript代码做了一点修改。也可以使用它。
    • 我已经在 web 服务的示例中将 C# 转换为 VB。你能告诉我它是否正确以及将 [ScriptService] 放在哪里?
    • 我不是一个 VB 人,但我认为你应该在顶部添加这行代码,Imports System.Web.Script.Services 和这个属性 _ 在类声明之上。 (在 _ 下方)
    猜你喜欢
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 2019-01-15
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多