2

Here's my function which returns a name of a person.

function getName(ID){
    return $.ajax({
        url:url,
        data: ID,
        type: 'get',
        dataType: 'json'
    });
}

And here is where I am attempting to use the .done() function.

getName("1").done() // How do I ref the returned var in our done function? 

I am confused on how to reference the returned value from my getName function within my done function.

I know I could do this using success: within the .ajax, but I am trying to move away from that and used deferred objects.

Thanks!

4

2 に答える 2

1

it look slike done requires a callback function that takes a parameter with the data recieved from teh request. http://api.jquery.com/jQuery.ajax/

getName("1").done(function(response) {
  alert(response);
}); 
于 2012-07-20T23:48:19.490 に答える
1

それはドキュメントにあります:

//Example: Save some data to the server and notify the user once it's complete.
$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

//Example: Retrieve the latest version of an HTML page.
$.ajax({
  url: "test.html",
  cache: false
}).done(function( html ) {
  $("#results").append(html);
});

また、これを必ずお読みください。データ型によっては、単純な文字列/ID ではなく、JSON/XML の結果から解析されたオブジェクトを実際に「受け取る」可能性があるため、または...

于 2012-07-20T23:52:45.590 に答える