开发者

Getting information from the server for a Gadget (Vista/7)

Hi all..

I want to get some information from my database and show into my sidebar gadget (i am newbee on gadget coding). I tried many ways to do it but i didn't succeed yet.

For this purpose, I have prepared a php file to get some values from my database (on the server) and i want to get the content (resu开发者_StackOverflow中文版lts) of this php file.

I don't want to use iframe because of visuality.

Actually, i can follow any way to do it. Such as;

Getting data from directly Database (wit security issues), getting specified text from php (values that are put in special divs) or getting all content of php and styling into sidebar with/without GET,POST methods or whatever it is :)

Thanks right now...

NOTE : i know (semi-proffesional) PHP, JavaScript, CSS but I don't know C#, VB etc.


There are a couple of ways you can access the data. The first is directly using ADO, a COM object built into Windows that can connect to databases. There are a few drawbacks to this method, the most important being that it's not very secure - your database username and password would have to be stored in plain text in the gadget's script file for anyone to see. This isn't quite as bad with limited permissions set.

Most developers would use a combination of PHP and XMLHttpRequest(). The PHP will fetch the data from the database on request, as you suggested yourself. The XMLHttpRequest is what is used for fetching the page contents. The only thing you need to decide is what format to output the data in; XML, JSON or something else. XML and JSON can be parsed by your gadget so either of those is a great choice. If you're running PHP 5.2 or later then JSON support is native. It's straightforward enough to get the data from the database as an associative array and then json_encode it and print the result.

XMLHttpRequest example

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://mysite.com/test.php", true);
xhr.onreadystatechange = function ()
{
    // readyState 4 = complete, status 200 = HTTP OK
    if (xhr.readyState == 4 && xhr.status == 200)
    {
        parseData(JSON.parse(xhr.responseText)); // parse a JSON response
        // parseData(xhr.responseXML); // parse an XML response
    }
}
xhr.send();

Gadgets running on machines with IE8 installed can use JSON.parse() natively, pre IE8 will need to either eval() the data or run it through a safe parser (most of which eval after making sure the data is valid JSON).

More reading:

XMLHttpRequest Object (MSDN)
JSON in JavaScript

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜