首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用javascript发送命令的ASP.net web服务

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

Stack Overflow用户
提问于 2012-07-23 15:44:33
回答 2查看 1.2K关注 0票数 0

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

我将使用Dreamweaver创建来自phonegap的“应用程序”。我正在寻找使用javascript发送一个字符串到一个web服务的例子,我可以在PC上不断地阅读,以便根据需要根据我在应用程序上按的按钮预编任务。

举个例子:

代码语言:javascript
复制
 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中这样做?:

代码语言: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来说是:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2012-07-23 15:53:40

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

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

您的web服务:

代码语言:javascript
复制
[WebMethod]
 //parameter name must match the parameter name passed in from the Ajax call (sometring)
 public static string PerformAction(string somestring) 
 {
   return "something";
 }

这里有详细的例子。

票数 1
EN

Stack Overflow用户

发布于 2012-07-23 16:38:37

您所做的是正确的,但是您需要用ScriptService属性来修饰您的webservice类。

代码语言:javascript
复制
[ScriptService]
  public class MyWebService : WebService {
   [WebMethod]
   public void MyMethod() {
   }
}

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

更新

稍微修改一下javascript函数。我不是使用“done”方法,而是设置传递给$.ajax方法的设置对象的“成功”属性。如果希望webservice调用是异步的,可以设置异步属性,如下面的代码所示。

代码语言:javascript
复制
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);
   }
   });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11615897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档