Search code examples
javascriptweb-serviceswinjs

How to return a value from WinJS


I am developing an app which consumes a WebService like this

function  callWS(filebytes, fpath, filename) { //consumes the webservice
    var response;
    var data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.myCompany.com">\n' +
                '   <soapenv:Header/>\n' +
                '       <soapenv:Body>\n' +
                '           <ws:uploadFileService>\n' +
                '           <ws:filebytes>' + filebytes + '</ws:filebytes>\n' +
                '           <ws:fpath>' + fpath + '</ws:fpath>\n' +
                '           <ws:filename>' + filename + '</ws:filename>\n' +
                '           </ws:uploadFileService>\n' +
                '       </soapenv:Body>\n' +
                '</soapenv:Envelope>\n';
    console.log("XML SOAP: " + data + "\r\n");

    var options = {
        url: "http://XXX.XXX.XX.XXX:XXXX/FILESERVERWS/services/FILESERVERWS?wsdl",
        type: "post",
        headers: {
            "Content-Type": "text/xml; charset=utf-8",
            "SOAPAction": "uploadFileService"
        },
        data: data
    };

    WinJS.Promise.timeout(8000, WinJS.xhr(options)).then(
        function (request) {
            var doc = request.responseXML.documentElement;
            var output = doc.getElementsByTagName("uploadFileServiceReturn");

            //Windows.UI.Popups.MessageDialog(output[0].textContent, "the XML message").showAsync();

            console.log("the XML message: " + output[0].textContent + "\r\n");

            result.style.backgroundColor = "#00A000";
            response = true;
        },
        function (error) {
            Windows.UI.Popups.MessageDialog(error.status + " : " + error.statusText, "Status").showAsync();
            result.style.backgroundColor = "#FF0000";

            response = false;
        },
        function (progress) {
            result.innerText = "Ready state is " + progress.readyState;
            result.style.backgroundColor = "#0000A0";
        }

        );
    return response;
}

the purpose is to consume the webService and returns a value

on success response = true on error response = false

because I want to take an action depending if the webService returned a value by doing this

 if (callWS(document.getElementById("formfield" + i).value, UID_KEY[7], arrayCaptures[i - 1].name)) {
                    console.log("take action a");
                }
                else {
                    console.log("take action b");
                }

but it always take action B even if the webService is consumed and I get answer from the webservice, what am I doing wrong???


Solution

  • You'll need to return a Promise object from your function, allowing the calling script to use a then() or done() call on it to get the result. You can read more about asynchronous programming in WinJS on the msdn site, but generally it looks like this:

    function  callWS(filebytes, fpath, filename) {
        return new WinJS.Promise(function (complete, error) {
            // put your functionality here...
    
            WinJS.Promise.timeout(8000, WinJS.xhr(options)).then(
                function (request) {
                    // more processing...
    
                    complete(true); // or false or a variable...
                },
                function (e) {
                    // error handling unique to this function
                    complete(false);  // OR you could just call error(e);
                },
                ...
            );
        });
    }
    

    And you would use it like this:

    callWS( ... ).then(
        function(response) {
            // handle response...
            // will be either true or false
        },
        function(err) {
            // handle errors
        }
    );