简体   繁体   中英

How can I trigger a Dojo xhrGet by clicking instead of on page load?

New to Dojo Toolkit, I'm able to successfully get an xhrGet to load on page load but I need to load the xhrGet when a row of a DataGrid is selected. Is there even a way to do so?

<!doctype html>
<html>
<head>
<script type="text/javascript>
dojo.connect(btn, "onclick", function() {
/************************/
/* xhr - AJAX functions */
/************************/
dojo.ready(function(){

var targetNode = dojo.byId("xhr-container");

var xhrArgs = {
url: "info-to-get.html",
handleAs: "text",
load: function(data){
targetNode.innerHTML = data;
},
error: function(error){
targetNode.innerHTML = "You messed up, dummy!: " + error;
}
}
var deferred = dojo.xhrGet(xhrArgs);
});
});
</script>
</head>
<body>
<button type="button" id="btn">TEST</button>
<br>
<br>
<div id="xhr-container"></div>
</body>
</html>

The xhrGet works fine by itself on page load, no dice when trying to call it through a button.

This will wire up the click of an html domNode to firing a function where you can put your xhr code.

dojo.connect(someDomNode, 'onclick', function() {
    //xhr code here
});

If it is a widget, like dijit.form.Button you can write

dojo.connect(someWidget, 'onClick', function() {
    //xhr code here
});

--EDIT ----------------------

Based on your posted code, I would expect the following to work:

dojo.ready(function(){

    var targetNode = dojo.byId("xhr-container");

    var fnDoXHR = function() {
        var xhrArgs = {
            url: "info-to-get.html",
            handleAs: "text",
            load: function(data){
                targetNode.innerHTML = data;
            },
            error: function(error){
                targetNode.innerHTML = "You messed up, dummy!: " + error;
            }
        };
        var deferred = dojo.xhrGet(xhrArgs);
    };

    dojo.connect(dojo.byId('btn'), 'onclick', fnDoXHR);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM