Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Ajax always returns "undefined"?

Tags:

jquery

ajax

  function getThisFrame(frameId) {
    var r;
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "abcdefg.asmx/RetriveThis",
        data: "{Id:" + Id + "}",
        dataType: 'json',
        success: function (result) {
               return result.d
        } 
    });
} 

The return value is always "undefined"? How could I solve this? Thanks!

The data is surely no problem!

like image 511
user386604 Avatar asked Jan 24 '26 21:01

user386604


1 Answers

you are returning result.d to $.ajax() not to getThisFrame().

You need somekind of callback if you want to handle result.d somehow.

function getThisFrame(frameId, callback) {
var r;
$.ajax({
    type: "POST",
    contentType: "application/json",
    url: "abcdefg.asmx/RetriveThis",
    data: "{Id:" + Id + "}",
    dataType: 'json',
    success: function (result) {
           if(typeof callback === 'function') callback.apply(this, [result.d]);
    } 
});
} 

getThisFrame(5, function(data){
    // do something with data.
});
like image 171
jAndy Avatar answered Jan 27 '26 11:01

jAndy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!