I'm making an ajax call with JQuery to read some XML and return some data. I successfully found my file, read stuff, and did some alerts and successful found the data i want. I want to now return this data to the function that's calling it, but not sure how.
Here's my function:
function getDialogParams(id, folderName) {
var dialogNode; // set variable here to open scope for return statement
$.ajax({
type: "GET",
url: "/" + folderName + "/dialog-params.xml",
dataType: "xml",
success: function (xml) {
alert("champions!"); // runs
$(xml).find('dialog').each(function () {
if ($(this).find('id').text() == id) { // finds my node i want
// runs once
dialogNode = $(this); // seems like im declaring a new variable instead of assigning my variable i instantiated above?
alert("width = " + dialogNode.find('width').text()); // alerts proper width
alert("height = " + dialogNode.find('height').text()); // alerts proper height
return;
}
});
},
error: function () { alert("fail whale!") }
});
alert("width2 = " + dialogNode.find('width').text()); // error dialogNode is undefined, wth, i declared this variable up-scope?
return dialogNode; // should be returning same object as above that alerts correct data
}
Then im using this function like this:
var params = getDialogParams(515, "AN0500-ASN"); // get param values
Where am i going wrong? Here's the XML in case someone wants to have it all to debug.
<?xml version="1.0" encoding="utf-8" ?>
<dialogs>
<!-- SHIPMENT SCREEN -->
<dialog>
<id>515</id>
<width>1000</width>
<height>700</height>
</dialog>
<!-- ORDER SCREEN -->
<dialog>
<id>516</id>
<width>900</width>
<height>600</height>
</dialog>
<!-- CARTON SCREEN -->
<dialog>
<id>517</id>
<width>800</width>
<height>500</height>
</dialog>
</dialogs>