开发者

Windows gadgets contacting webservices

Is it possible to create a Windows Gadget, which contacts a web service.

In this case calls the web service method which in turn开发者_JS百科 returns the results of a pre-defined SQL Server query.

If so, what is an ideal approach to doing this.

Has anyone any experience in doing such a thing?


To answer your question, this is indeed very possible and fairly simply too.

On the client/gadget side you will need to use javascript to make an ajax request to your server. when this request is made your server will simply access your SQL database and print it, creating the response to that ajax call. Here is an example using php as the server side language. Please Comment if you need help.

Client side (javascript):

function GetContent(){
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        var response = xmlhttp.responseText;
                document.getElementsById("RESULTS DIV").innerHTML = response
                //or something else to be done with response
        }
      }
    var time1 = new Date().getTime()

    xmlhttp.open("GET","PHP SERVER ADDRESS?time="+time1,true);

        // the time parameter is important to prevent caching the response - 
        //   your server code does not need to handle it.
    xmlhttp.send();
    }

And The Server code in php

   <?php
    $con = mysql_connect("MYSQL SERVER ADDRESS","USERNAME","PASSWORD");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM TABLE_NAME");

while($row = mysql_fetch_array($result))
  {
  echo $row['COLUMN NAME'];
  echo "<br />";
  }

mysql_close($con);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜