简体   繁体   English

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

[英]ASP.net web service using javascript to send commands

I am looking to send commands to a web service page on my local network via javascript that's being used on a WebOS tablet. 我希望通过在WebOS平板电脑上使用的javascript将命令发送到本地网络上的Web服务页面。 Problem being is that I've never done this before and was wondering if it was even possible to do in the first place? 问题在于我以前从未这样做过,并且想知道是否有可能首先做到这一点? Is it possible for me to create a VB.net to listen to a web service call/webpage? 我可以创建一个VB.net来收听网络服务电话/网页吗?

I would be creating the "app" from phonegap using Dreamweaver. 我将使用Dreamweaver从phonegap创建“app”。 I am looking for examples of using javascript to send over a string to a web service that i can read constantly on the PC in order to preform tasks as needed depending on the button i push on the app. 我正在寻找使用javascript通过字符串发送到Web服务的示例,我可以在PC上不断阅读,以便根据我推送应用程序的按钮根据需要执行任务。

So as an example: 举个例子:

 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

Does anyone have an example of something like this? 有没有人有这样的例子?

update So i would do something like so in my javascript?: 更新所以我会在我的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>

and for my WS it would be: 对于我的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

Yes, you can call a Web Service from Javascript via ajax. 是的,您可以通过Ajax从Javascript调用Web服务。 One example is this: 一个例子是:

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

Your web service: 您的网络服务:

[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. 详细示例在这里。

What you are doing is correct, but you need to decorate your webservice class with the [ScriptService] attribute. 你正在做的是正确的,但你需要使用[ScriptService]属性来装饰你的webservice类。

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

Once this is done you can access the web service via $.ajax like you have done. 完成后,您可以通过$ .ajax访问Web服务,就像您已经完成的那样。

Update 更新

Making a slight change to your javascript function. 对您的javascript函数稍作修改。 Instead of using the 'done' method, I am setting the 'success' property of the settings object passed to the $.ajax method. 我没有使用'done'方法,而是设置传递给$ .ajax方法的设置对象的'success'属性。 If you want the webservice call to be asynchronous, you can set the async property as shown in the code below. 如果您希望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);
   }
   });
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM