开发者

setting jquery.get() to a variable not working

I'm writing a page that will print badge labels for members. It's a DYMO labelmaker, and I'm using their javascript label framework.

All the members are listed with checkboxes, with the data to be encoded for the badge in the value. The user will check the members they want to print badges for, and click print.

The script will grab the checked checkboxes, which it does, and pass the value through a get request to a separate php file that will return the encoded data, and it does.

The data to be returned is period delimited, which I need to split and put each piece on a separate line. The problem is I can't seem to get the data out of the scope of the get.

The pertinent portion of the script:

printButton.onclick = function() {
    try {
        printButton.disabled = true;
        settings.currentPrinterName = printersComboBox.value;
        var printer = printers[settings.currentPrinterName];
        if (!printer)
            throw new Error("Select printer");
        var label = null;
        if (printer.printerType == "LabelWriterPrinter") {
            label = addressLabel;
        }
        if (!label)
            throw new Error("Label is not loaded. Wait until is loaded or reload the page");
        var labelSet = new dymo.label.framework.LabelSetBuilder();
        var barcode
        $("#memchk :checked").each(function(){
            var value = $(this).val();
            var barcode;
            var record = labelSet.addRecord();
            $.get("http://ranch/sunrise/wolf/plugins/member_directory/views/barcode.php",{encString: value}, function(data){
                //alert(data.split("."));
                barcode = data.split(".");
            });
            alert(barcode[0]);
   开发者_开发技巧         record.setText("TEXT", barcode[0]);
            record.setText("TEXT_1", barcode[1]);
            record.setText("TEXT_2", barcode[2]);
            record.setText("TEXT_3", barcode[3]);
            record.setText("TEXT_4", barcode[4]);
            record.setText("TEXT_5", barcode[5]);
            record.setText("TEXT_6", barcode[6]);
            record.setText("TEXT_7", barcode[7]);
            record.setText("TEXT_8", barcode[8]);
            var memName = value.split("^");
            record.setText("TEXT_9", memName[0]);
        });
        //label.print(printer.name, null, labelSet.toString());
        saveSettings();
    } catch(e) {
        printButton.disabled = false;
        alert(e);
    }
    printButton.enabled = true;
}

I've tried setting the text for each line inside the get, but it doesn't take. The alert inside the get displays the expected data, but if i try to run it as it, it says barcode is undefined. If I comment out the get and set the text lines of the label to static strings, it works fine.

Why can I not get the data?


instead of get, try this:

var barcode =  $.ajax({
        url: "http://ranch/sunrise/wolf/plugins/member_directory/views/barcode.php",
        type: "GET",
        data: {encString: value},
        async: false
    }).responseText;

and you'll have barcode assigned with your request, and this:

        alert(barcode[0]);
        record.setText("TEXT", barcode[0]);
        record.setText("TEXT_1", barcode[1]);
        record.setText("TEXT_2", barcode[2]);
        record.setText("TEXT_3", barcode[3]);
        record.setText("TEXT_4", barcode[4]);
        record.setText("TEXT_5", barcode[5]);
        record.setText("TEXT_6", barcode[6]);
        record.setText("TEXT_7", barcode[7]);
        record.setText("TEXT_8", barcode[8]);

should work ;)


get is simply a shorter way of making an AJAX get request. AJAX is asynchronous (that's what the A stands for). This means that the request is sent and the response comes back sometime later. All of the stuff you do after the get needs to be moved into the callback function if you want it to be executed after the response is received.


Not sure what version of jQuery you are using, but I would start with the docs here:

http://api.jquery.com/jQuery.get/

Also these are not my slides (thanks Dan Heberden) but here is a good example of a callback:

http://danheberden.com/presentations/deferreds-putting-laziness-to-work/#4

So you want to make sure that you call the function to process your data from within your callback function (the part with barcode = data.split(".")... The slides above also show how to use Deferred objects to chain multiple callbacks in a much cleaner way. I recommend that you go through this and the Deferred objects documentation here:

http://api.jquery.com/category/deferred-object/

In short, you should be able to solve your problem using callbacks properly, but for a more readable solution, do check out deferreds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜