0
var getPageInhalt : function (id){
    var restmethod = "http://localhost:1212/getPageById/"+id+"/jsonp.aspx?callback=?";

    $.ajax({
        url: restmethod,
        type: "GET",
        contentType: "application/json",
        async: false,                        
        dataType: 'jsonp',
        cache: false,
        error: function(){
            return false;
        },
        success: function(data){ 
            console.log(data); --> balblal
            return data;
        }
    });
}

console.log(getPageInhalt(2));-->undefined ?????
4

1 回答 1

2

虽然我不同意同步 AJAX(从技术上讲它是矛盾的 ..),但您将不得不从getPageInhalt函数中返回值,而不是从 ajax 回调中返回值。

var getPageInhalt = function (id){
    var restmethod = "http://localhost:1212/getPageById/"+id+"/jsonp.aspx?callback=?",
        resultValue;

    $.ajax({
        url: restmethod,
        type: "GET",
        contentType: "application/json",
        async: false,                        
        dataType: 'jsonp',
        cache: false,
        error: function(){
            resultValue = false;
        },
        success: function(data){ 
            console.log(data); //--> balblal
            resultValue = data;
        }
    });

    return resultValue;
}

这是一个演示 http://jsfiddle.net/gaby/qHY7g/

于 2012-06-11T09:32:38.923 回答