tags:

views:

19

answers:

3

Is it possible to interact with an XML file after making a $.ajax() call?

IE:

.$ajax( 
url: "someUrl", 
type: "GET", 
dataType:"xml", 
success: function(xml){ //some function },
error : function(){ //some function}
});

$("#somebutton").click(function(xml){ //function that interacts with XML });

I haven't been able to interact with any XML file's unless all the functions are inside the success parameter. Any tips, or do I just need to put all my functions into the success function? Is there a better way to do what I'm describing than using $.ajax()

+1  A: 

the success function is a closure that executes when a response has come back from the server. The variable, xml is only valid within the scope of the function. What you CAN do is a few things:

  1. put your handlers into the success function
  2. create a variable OUTSIDE of the ajax call, and in the success function assign the value of XML to that variable

so:

var xmlObj = "";
$.ajax({
  success:function(xml) {
    xmlObj = xml;
  }
});

alert(xmlObj);
Anatoly G
Wow, I didn't even think about assigning xml to a global variable. Thanks!
Ian
+1  A: 

The functions don't have to be in the success function to use it, but the xml varible only exists inside the success functions scope you would have to set another variable to it.

Doesn't work

$.ajax( 
  url: "someUrl", 
  type: "GET", 
  dataType:"xml", 
  success: function(xml){ //some code },
  error : function(){ //some code}
});

function parse() {
  //xml processing code
}

Works

var myXML;

$.ajax( 
  url: "someUrl", 
  type: "GET", 
  dataType:"xml", 
  success: function(xml){ 
    myXML = xml;
    //some code 
  },
  error : function(){ //some code}
});

function parse() {
  //myXML processing code
}

Also if you would call parse before the AJAX call has completed successfully, then the myXML variable would still be null. Could always perform a check.

function parse() {
  if (myXML) {
    //myXML processing code
  } else {
    //ajax not completed successfully yet
  }
}
CastroXXL
Thanks for the additional information on the global variable. Another question: doesn't "if(myXML)" just check to see if the variable has been initialized? Or does it check to see if the variable has a value to it?
Ian
It checks to make sure the variable is not empty and does not equal 0 or false. So undefined, null, "", 0, and false would all result in false. Just a quick way to check. Not always the best(depending on the situation), but it will do the job.
CastroXXL
+1  A: 

You can also do it this way which doesn't require global variables:

$.ajax({ 
    url: "someUrl", 
    type: "GET",
    dataType:"xml", 
    success: function(xml){
        $("#somebutton").bind("click",{xmlData:xml}, buttonClick);
    },
    error : function(){ /*some code*/ }
});

function buttonClick(event) {
    var xml = event.data.xmlData;
    //function that interacts with XML
}
Drackir
Thanks for the answer. I think that setting xml to a global variable will be a better solution for me for this project, but I'll keep this method in mind for future projects! It's great to know of many ways to accomplish these things!
Ian