0

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
StealthRT
  • 10,108
  • 40
  • 183
  • 342

2 Answers2

1

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.

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • 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
  • You can also do it from the WS itself but you'd need to run the WS on a privileged account and I am not sure that's such a good idea... – Icarus Jul 23 '12 at 16:34
  • @lcarus: The PC is just a home entertainment PC so setting the privileges would not be a problem or a risk. If i am able to do all i can do in VB.net inside the WS then i'll go with that one. However, the DB approach would work as well... just wouldn't like the idea of hammering the SQL server constantly and eating up resources every time it did it. – StealthRT Jul 23 '12 at 16:44
  • @StealthRT Hammering the DB shouldn't be a concern -unless you are using Access ;)- Connections to the DB are pooled; it's not like you are recreating 100s of physical connections. If it's only one single VB.net app hitting the DB, you will almost certainly end up creating 1 real connection even if you open 100 in you program because they are reused. Again, I wouldn't be concerned about that at all. Just make sure you dispose properly of the connection; using `using` blocks. – Icarus Jul 23 '12 at 17:09
1

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);
   }
   });
}
TWickz
  • 622
  • 6
  • 13
  • Thanks for the addition to that code. I have updated the OP to reflect that. – StealthRT Jul 23 '12 at 16:41
  • 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
  • I am not a VB guy but I think you should add this line of code at the top, Imports System.Web.Script.Services and this attribute _ above the class declaration. (below _ ) – TWickz Jul 23 '12 at 17:32
  • Well it did not work when i ran it from my computer using the javascript to send to the web service. I get an error of **"NetworkError: 500 Internal Server Error - http://localhost:51954/WebSite1/Service.asmx/PerformAction"** and that page is running just fine in my browser during testing. – StealthRT Jul 24 '12 at 03:08
  • Ok i think i got it working a little bit by just doing **Service.asmx/PerformAction**. However i am getting the dreaded error of **Access to restricted URI denied" code: "1012" nsresult: "0x805303f4 (NS_ERROR_DOM_BAD_URI)"** – StealthRT Jul 24 '12 at 04:01
  • Are you running the webservice on IIS ? Anyway refer to this url as well. http://stackoverflow.com/questions/51283/access-to-restricted-uri-denied-code-1012 Exact same problem :-) Once you get things online please mark the answer as correct ^_^ – TWickz Jul 24 '12 at 05:23
  • **Jsonp does not support post** – StealthRT Jul 24 '12 at 12:46