我希望通过在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发布于 2012-07-23 15:53:40
是的,您可以通过ajax从Javascript调用Web服务。一个例子是:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "WebService.asmx/PerformAction",
data: { "somestring" : "IE"},
dataType: "json"
});您的web服务:
[WebMethod]
//parameter name must match the parameter name passed in from the Ajax call (sometring)
public static string PerformAction(string somestring)
{
return "something";
}这里有详细的例子。
发布于 2012-07-23 16:38:37
您所做的是正确的,但是您需要用ScriptService属性来修饰您的webservice类。
[ScriptService]
public class MyWebService : WebService {
[WebMethod]
public void MyMethod() {
}
}一旦完成,您就可以像以前一样通过$.ajax访问web服务。
更新
稍微修改一下javascript函数。我不是使用“done”方法,而是设置传递给$.ajax方法的设置对象的“成功”属性。如果希望webservice调用是异步的,可以设置异步属性,如下面的代码所示。
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);
}
});
}https://stackoverflow.com/questions/11615897
复制相似问题