Javascript 如何从WinJS返回值

Javascript 如何从WinJS返回值,javascript,web-services,winjs,Javascript,Web Services,Winjs,我正在开发一个应用程序,它使用这样的Web服务 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

我正在开发一个应用程序,它使用这样的Web服务

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

但是它总是采取行动B,即使Web服务被使用,并且我从Web服务得到了答案,我做错了什么?

您需要从函数返回一个
Promise
对象,允许调用脚本使用
then()
done()
调用它以获得结果。您可以在msdn网站上了解更多信息,但通常情况下如下所示:

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);
            },
            ...
        );
    });
}
callWS( ... ).then(
    function(response) {
        // handle response...
        // will be either true or false
    },
    function(err) {
        // handle errors
    }
);
你可以这样使用它:

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);
            },
            ...
        );
    });
}
callWS( ... ).then(
    function(response) {
        // handle response...
        // will be either true or false
    },
    function(err) {
        // handle errors
    }
);