Show me the code! – By Davanum Srinivas

Web Services, Apache, Websphere, IBM, etc.

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

with one comment

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");
    }
})

Written by Davanum Srinivas

September 9, 2008 at 9:24 pm

Posted in Uncategorized

One Response

Subscribe to comments with RSS.

  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

    anjan bacchu

    September 9, 2008 at 11:20 pm


Leave a Reply