Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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. 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?

I would be creating the "app" from phonegap using Dreamweaver. 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.

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?:

<!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:

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
share|improve this question
add comment

2 Answers

Yes, you can call a Web Service from Javascript via ajax. 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.

share|improve this answer
 
I have updated my OP. How would i check for a new value sent via vb.net on the PC side in order to start "IE"? –  StealthRT Jul 23 '12 at 16:19
 
@StealthRT I don't understand your question. The Client sends what2Pass to the WS; the WS returns something. What is it that you want to know? –  Icarus Jul 23 '12 at 16:22
 
@lcarus: in order for me to do some custom stuff in the background (open relay ports, start .exe's, etc etc) i will need to do that in VB.net. How can i set up my VB.net program to monitor the web service and listen for those commands being sent? –  StealthRT Jul 23 '12 at 16:28
 
Or can i do all that i currently am able to do in VB.net on the web service page itself? –  StealthRT Jul 23 '12 at 16:33
 
I see now. You can't listen for the commands being sent to the WS. Why don't you have the WS publish the commands it gets to some sort of Message Queue and then have the VB.net program read from that Queue? One quick example: WS receives command; inserts into a DB table after validation, etc. The VB.net constantly monitors the DB table for new records inserted in the table and does whatever it needs. The VB.net can be on a timer or something, constantly polling for new records. –  Icarus Jul 23 '12 at 16:33
show 3 more comments

What you are doing is correct, but you need to decorate your webservice class with the [ScriptService] attribute.

[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.

Update

Making a slight change to your javascript function. Instead of using the 'done' method, I am setting the 'success' property of the settings object passed to the $.ajax method. If you want the webservice call to be asynchronous, you can set the async property as shown in the code below.

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);
   }
   });
}
share|improve this answer
 
Thanks for the addition to that code. I have updated the OP to reflect that. –  StealthRT Jul 23 '12 at 16:41
 
You're welcome. Did it work ? It should :-) –  TWickz Jul 23 '12 at 16:43
 
I'm not able to test it out right now but trust me, if it work or not ill be sure to post it for others to know! –  StealthRT Jul 23 '12 at 16:44
 
Thanks. Also I made a slight modification to the javascript code just now. Use it as well. –  TWickz Jul 23 '12 at 16:49
 
I have converted the C# to VB in the example of the web service. Can you let me know if its correct and where to place the [ScriptService] at? –  StealthRT Jul 23 '12 at 16:53
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.