Show me the code! – By Davanum Srinivas

September 9, 2008

[Ubiquity] HOWTO invoke a web service (SOAP 1.2) from ubiquity

Filed under: Uncategorized — Davanum Srinivas @ 9:24 pm

Sample to call the web service documented here:
CDYNE Weather – FREE

jQuery.ajax makes it real simple to do a SOAP 1.2 call and to parse the response xml as well.

Here’s the source code:

var noun_type_zip = {
    _name : "zip",
    suggest: function(fragment) {
        var regexp = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
        if (regexp.test(fragment)) {
            return [CmdUtils.makeSugg(fragment)];
        }
        return [];
    }
};

CmdUtils.CreateCommand({
    name: "weather-by-zip",
    takes: {"zip code": noun_type_zip},
    execute: function(directObj) {
    },

    preview: function(pblock, directObj) {
        payload = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" ' +
                  '               xmlns:weat="http://ws.cdyne.com/WeatherWS/"> ' +
                  '    <soap:Body> ' +
                  '        <weat:GetCityWeatherByZIP> ' +
                  '            <weat:ZIP>' + directObj.text + '</weat:ZIP> ' +
                  '        </weat:GetCityWeatherByZIP> ' +
                  '     </soap:Body> ' +
                  '</soap:Envelope> ';
        jQuery.ajax({
            type:     'POST',
            url:      "http://ws.cdyne.com/WeatherWS/Weather.asmx",
            dataType: "xml",
            data:     payload,
            beforeSend: function(req) {
                req.setRequestHeader("Content-Type", 'application/soap+xml;charset=UTF-8;action="http://ws.cdyne.com/WeatherWS/GetCityWeatherByZIP"');
            },
            success: function(xml) {
                var el = jQuery(xml).find("GetCityWeatherByZIPResult");
                pblock.innerHTML = "<p><table>" +
                                   "<tr><td> State </td><td>" + el.find('State').text() + "</td>" +
                                   "<tr><td> City </td><td>" + el.find('City').text() + "</td>" +
                                   "<tr><td> WeatherStationCity </td><td>" + el.find('WeatherStationCity').text() + "</td>" +
                                   "<tr><td> Description </td><td>" + el.find('Description').text() + "</td>" +
                                   "<tr><td> Temperature </td><td>" + el.find('Temperature').text() + "</td>" +
                                   "<tr><td> RelativeHumidity </td><td>" + el.find('RelativeHumidity').text() +  "</td>" +
                                   "<tr><td> Wind </td><td>" + el.find('Wind').text() +  "</td>" +
                                   "<tr><td> Pressure </td><td>" + el.find('Pressure').text() +  "</td>" +
                                   "<tr><td> Visibility </td><td>" + el.find('Visibility').text() +  "</td>" +
                                   "<tr><td> WindChill </td><td>" + el.find('WindChill').text() +  "</td>" +
                                   "</table></p>" ;
            },
            error: function() {
                displayMessage('ERROR: SOAP call failed!');
            }
        }, "xml");
    }
})



1 Comment »

  1. hi there,

    some of this functionality(and more) is available from MS (has been available as well for looooooooong time) in Webservice.htc — http://msdn.microsoft.com/en-us/library/ms531032(VS.85).aspx.

    Does anyone know how both of them compare in terms of stability and performance ?

    thank you,

    BR,
    ~A

    Comment by anjan bacchu — September 9, 2008 @ 11:20 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: