如何将值发布到WebService并获取另一个值? HTML

时间:2017-10-06 15:36:19

标签: javascript android web-services post

我想从条形码扫描的结果中使用我的'result.text'来搜索web服务以获取'frn2'。这是一个Android应用程序,我对此很新,所以如果你需要更多信息,我可以提供它。

JS:

       <script>
     function scan() {
    cordova.plugins.barcodeScanner.scan(
            function(result) {
                console.log(result.text)
                var barcode_res = (" ID\n" +
                    "Result: " + result.text);
                window.alert("ID Result: " + result.text);
            },
            function(error) {
                window.alert("Scanning Failed: " + error);
            },




            //getFRNbyMATRIC webservice call
            function(webserviceCall) {
                var string = result.text;
                $.post("http://localhost:8081/WebService.asmx?
                  op=getFRNByMATRIC", {
                    MATRIC: string
                }, function(response) {
                    frn2 = response;
                }).error(function() {
                    window.alert("Sorry could not proceed");
                });
                //return DCID Number
                return frn2;
            }

        )

    window.open("https://example.com/?frn=" + frn2);

         }
        </script>

XML / WEBSERVICE:

         <?xml version="1.0" encoding="utf-8"?>
<ArrayOfTEST1 xmlns="http://tempuri.org/">
 <TEST1>
   <ID>int</ID>
   <FRN>int</FRN>
   <FRN2>string</FRN2>
   <MATRIC>int</MATRIC>
   <numbers>
     <ID>int</ID>
     <Numbers>string</Numbers>
     <TEST1s>
       <TEST1 d5p1:nil="true" xmlns:d5p1="http://www.w3.org/2001/XMLSchema-
instance" />
       <TEST1 d5p1:nil="true" xmlns:d5p1="http://www.w3.org/2001/XMLSchema-
instance" />
     </TEST1s>
   </numbers>
 </TEST1>

1 个答案:

答案 0 :(得分:1)

我会用'承诺'来清理它,这样的事情可能有用:

function scan() {
    cordova.plugins.barcodeScanner.scan(
        function(result) {
            console.log(result.text)
            var barcode_res = (" ID\n" +
                "Result: " + result.text);
            getFrn(result.text)
                .then(function(frn2){
                    // you can do something with frn2 here
                    window.open("https://example.com/?frn=" + frn2);
                });
            window.alert("ID Result: " + result.text);
        },
        function(error) {
            window.alert("Scanning Failed: " + error);
        });

    //getFRNbyMATRIC webservice call
    function getFrn(str) {
        return new Promise(function(resolve, reject) {
            $.post("http://localhost:8081/WebService.asmx?op=getFRNByMATRIC", {
                MATRIC: str
            }, function(response) {
                frn2 = response;
                resolve(frn2);
            }).error(function(e) {
                window.alert("Sorry could not proceed");
                reject(e);
            });
        })
    }
}