ASP.net web服务使用javascript发送命令

ASP.net web service using javascript to send commands

本文关键字:javascript 命令 net web 服务 ASP      更新时间:2024-01-23

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

我会使用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

是的,您可以通过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";
 }

这里有详细的例子。

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

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

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

更新

对您的javascript函数进行一点更改。我没有使用"done"方法,而是设置传递给$.ajax方法的设置对象的"success"属性。如果您希望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);
   }
   });
}