通过条形码扫描器phonegap检查条形码是否有效

时间:2015-04-21 11:08:26

标签: json cordova barcode barcode-scanner

我使用此条形码扫描仪:https://github.com/wildabeast/BarcodeDemo来扫描条形码。这工作正常但现在我想检查条形码是否由json有效。

我现在做了这个,但它不起作用。我怎么能用$ .get来检查条形码是否有效?请求页面工作正常,这是代码中的内容:

 function() {
    console.log('scanning');

    var scanner = cordova.require("cordova/plugin/BarcodeScanner");

    scanner.scan( function (result) { 

        alert("We got a barcode\n" + 
        "Result: " + result.text + "\n" + 
        "Format: " + result.format + "\n" + 
        "Cancelled: " + result.cancelled);  

        var barcode = result.text;

        $.get("https://url.com/request.php",{ data:barcode },
        function(data){

            if(data.status=='ok'){
                // barcode is valid

            } else {


            }
        }, "json");

       console.log("Scanner result: \n" +
            "text: " + result.text + "\n" +
            "format: " + result.format + "\n" +
            "cancelled: " + result.cancelled + "\n");
        document.getElementById("info").innerHTML = result.text;
        console.log(result);
        /*
        if (args.format == "QR_CODE") {
            window.plugins.childBrowser.showWebPage(args.text, { showLocationBar: false });
        }
        */

    }, function (error) { 
        console.log("Scanning failed: ", error); 
    } );
},

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码进行ajax调用,而不是$.get

$.ajax({
    type       : "POST",
    url        : "https://url.com/request.php",
    crossDomain: true,
    beforeSend : function() {$.mobile.loading('show')},
    data       : {barcode: barcode},
    dataType   : 'json',
    success    : function(response) {
             //check here your responce 

      //  console.error(JSON.stringify(response));

    },
    error      : function() {
        //console.error("error");
        alert('Now working!');                  
    }
});