weixin_33720956 2012-08-23 04:38 采纳率: 0%
浏览 23

管道值:Ajax + JQuery

I have a function in which I execute an ajax request and wait till I get a response and return a value but the value returned is undefined. What is wrong?

            function GetVMData(url_s){ 
                return $.ajax({ 
                       url: url_s, 
                       crossDomain: true,  
                       dataType: 'jsonp', 
                       error: function(xhr, status, error) {  
                           alert('failed') 
                        }  
                   }).pipe(function(data) { return data[4]; }); 
            } 

If I print the value of data[4] within the ajax callback it prints the right value, therefore i know the request is going through but when I try this:

            var cord;
            cord = GetVMData(url).done(function(cpu_USG) { 
            return cpu_USG;
            }); 
            alert(cord)

the value of cord is wrong.

  • 写回答

2条回答 默认 最新

  • weixin_33688840 2012-08-23 04:42
    关注
        var cord;
        cord = GetVMData(url).done(function(cpu_USG) { 
        return cpu_USG;
        }); 
        alert(cord)
    

    This code runs asynchronously. So you need to perform everything in the callback, like:

        GetVMData(url).done(function(cpu_USG) { 
            alert(cpu_USG);
        }); 
    
    评论

报告相同问题?