返回“成功:”方法的数据?

I'm trying to get the return value of a utility method I wrote using jquery:

function loadFrames(coords, spritesheet) {
    return $.ajax({
        type: "GET",
        url: coords,
        dataType: "xml",
        success: function(xml,code,obj) {return parseFrameData(xml, spritesheet);}
    });
}

So, this method receives two arguments, opens a file (the one pointed to by the "plist" argument) and runs the parseFrameData method. The latter returns an array of objects.

I would like to use this the following way:

var frames = loadFrames('player.xml', 'spritesheet.png');

but I don't find the way to say "return the value of the method you called on line starting with "'success:' "...

Ajax runs asynchronously (meaning in the background, allowing other things to continue) so you wont be able to directly return a value from it.

How ever, you can pass the function a callback that will be called once the Ajax request is finished.

function loadFrames(coords, spritesheet, callback) {
    $.ajax({
        type: "GET",
        url: coords,
        dataType: "xml",
        success: function(xml,code,obj) { callback(parseFrameData(xml, spritesheet)); }
    });
}

Also, on a side note, dont forget to check for HTTP errors like 404 and 500 by passing a error paramter to the ajax call

Edit: An answer posted to the question @Tomalak linked to informed me of this cool concept of 'Promises', which you might also want to take a look at.

The point of using $.ajax is generally to make asynchronous requests. Calling it and expecting a value to be returned immediately would be a synchronous request.

That being said, you can do what you're asking by using $.ajax in synchronous mode by setting async:false. This may have the affect of locking your browser until the request completes.

function loadFrames(coords, spritesheet) {
  var myFrames;

  $.ajax({
    type: "GET",
    url: coords,
    dataType: "xml",
    async: false,
    success: function(xml,code,obj) {myFrames = parseFrameData(xml, spritesheet);}
  });
  return myFrames;
}

var frames = loadFrames('player.xml', 'spritesheet.png');

I believe that is will work, but I don't have a good way to test it. Can anyone else confirm this approach?

Still it would be much better to do this asynchronously the way @Petah suggests.