来自自定义jQuery AJAX函数的Javascript回调

来自自定义jQuery AJAX函数的Javascript回调,javascript,jquery,ajax,json,callback,Javascript,Jquery,Ajax,Json,Callback,我有这个jQuery代码 (function () { function load_page (pagename) { $.ajax({ url: "/backend/index.php/frontend/pull_page/", type: "POST", data: {page: pagename}, success: function (json) {

我有这个jQuery代码

(function () {
    function load_page (pagename) {
        $.ajax({
            url: "/backend/index.php/frontend/pull_page/",
            type: "POST",
            data: {page: pagename},
            success: function (json) {
                var parsed = $.parseJSON(json);
                console.log(parsed);
                return parsed;
            },
            error: function (error) {
                $('#content').html('Sorry, there was an error: <br>' + error);
                return false;
            }
        });
    }
    ...
    var json = load_page(page);
    console.log(json);
    if (json == false) {
        $('body').fadeIn();
    } else {
        document.title = json.pagename + ' | The Other Half | freddum.com';
        $("#content").html(json.content);
        $('#header-navigation-ul a:Contains('+page+')').addClass('nav-selected');
        $('body').fadeIn();
    }
})();
我将
load_page
移动到全局名称空间中,因为我需要它在那里。
console.log(parsed)
返回看似有效的json对象,但
console.log(parsed.content)
产生
未定义的
<代码>#内容
也未设置。有什么想法吗?我很乐意做任何测试

非常感谢您的帮助

因为Ajax请求是异步的,所以无论请求是否完成,
$.Ajax
函数调用后的代码仍然会执行,因此您应该接受回调作为请求完成时调用的
load\u page
的参数:

function load_page (pagename, callback) {
    $.ajax({
        url: "/backend/index.php/frontend/pull_page/",
        type: "POST",
        data: {page: pagename},
        success: function (json) {
            var parsed = $.parseJSON(json);
            console.log(parsed);
            callback(parsed); //bingo
        },
        error: function (error) {
            $('#content').html('Sorry, there was an error: <br>' + error);
        }
    });
}

load_page(page, function(json) {
   console.log(json);
   if (json == false) {
      $('body').fadeIn();
   } else {
      document.title = json.pagename + ' | The Other Half | freddum.com';
      $("#content").html(json.content);
      $('#header-navigation-ul a:Contains('+page+')').addClass('nav-selected');
      $('body').fadeIn();
   }
});
函数加载\页面(页面名,回调){
$.ajax({
url:“/backend/index.php/frontend/pull_page/”,
类型:“POST”,
数据:{page:pagename},
成功:函数(json){
var parsed=$.parseJSON(json);
console.log(已解析);
回调(已解析);//宾果
},
错误:函数(错误){
$(“#content').html('抱歉,出现错误:
'+错误); } }); } 加载页面(页面,函数(json){ log(json); if(json==false){ $('body').fadeIn(); }否则{ document.title=json.pagename+“|另一半| freddum.com”; $(“#content”).html(json.content); $(“#标题导航ul a:Contains(“+page+”)).addClass('nav-selected'); $('body').fadeIn(); } });
因为Ajax请求是异步的,所以无论请求是否完成,
$函数调用之后的代码仍然会执行,因此您应该接受回调作为请求完成时调用的
load\u page
的参数:

function load_page (pagename, callback) {
    $.ajax({
        url: "/backend/index.php/frontend/pull_page/",
        type: "POST",
        data: {page: pagename},
        success: function (json) {
            var parsed = $.parseJSON(json);
            console.log(parsed);
            callback(parsed); //bingo
        },
        error: function (error) {
            $('#content').html('Sorry, there was an error: <br>' + error);
        }
    });
}

load_page(page, function(json) {
   console.log(json);
   if (json == false) {
      $('body').fadeIn();
   } else {
      document.title = json.pagename + ' | The Other Half | freddum.com';
      $("#content").html(json.content);
      $('#header-navigation-ul a:Contains('+page+')').addClass('nav-selected');
      $('body').fadeIn();
   }
});
函数加载\页面(页面名,回调){
$.ajax({
url:“/backend/index.php/frontend/pull_page/”,
类型:“POST”,
数据:{page:pagename},
成功:函数(json){
var parsed=$.parseJSON(json);
console.log(已解析);
回调(已解析);//宾果
},
错误:函数(错误){
$(“#content').html('抱歉,出现错误:
'+错误); } }); } 加载页面(页面,函数(json){ log(json); if(json==false){ $('body').fadeIn(); }否则{ document.title=json.pagename+“|另一半| freddum.com”; $(“#content”).html(json.content); $(“#标题导航ul a:Contains(“+page+”)).addClass('nav-selected'); $('body').fadeIn(); } });
在解析之前,请尝试console.log,以检查到底是什么数据。它是有效的json吗

成功:函数(json){ log(json);
var parsed=$.parseJSON(json);

在解析之前尝试console.log以检查到底是什么数据。它是有效的json吗

成功:函数(json){ log(json);
var parsed=$.parseJSON(json)这是一个AJAX调用,如中所示,代码是异步完成的。您需要将console.log和json变量的任何其他用法放入success函数中。

这是一个AJAX调用,如中所示,代码是异步完成的。您需要将console.log和json变量的任何其他用法放入success函数中。

这是因为jQuery在默认情况下异步发出ajax调用。因此,下一条语句甚至在ajax调用完成后执行
var json=load_页面(第页);。
您可以通过在配置参数中传递async:false并在回调函数中处理retun值来使调用同步。

问题在于jQuery默认异步发出ajax调用。因此,下一条语句甚至在ajax调用完成后执行
var json=load_页面(第页);。
您可以通过在配置参数中传递async:false并在回调函数中处理retun值来同步调用。

在load\u page函数的定义中没有“return”语句,至少通过执行
var json=load\u page(page)来实现
您将以json=undefined结束。理想情况下,您应该稍微重新组织代码。实现这一点的方法不止一种,但这里有一种:

(function () {
    function mySuccess(json) {
        var parsed = $.parseJSON(json);
        console.log(json);
        console.log(parsed);
        document.title = parsed.pagename + " | The Other Half | freddum.com";
        $("#content").html(parsed.content);
        $("#header-navigation-ul a:Contains(" + page + ")").addClass("nav-selected");
        $("body").fadeIn();
    }
    function myFailure(error) {
        $('#content').html('Sorry, there was an error: <br>' + error);
        $("body").fadeIn();
    }
    function load_page(pagename, onSuccess, onFailure) {
        $.ajax({
            url: "/backend/index.php/frontend/pull_page/",
            type: "POST",
            data: {
                page: pagename
            },
            success: onSuccess,
            error: onFailure
        });
    }
    load_page(page, mySuccess, myFailure);
})();
(函数(){
函数mySuccess(json){
var parsed=$.parseJSON(json);
log(json);
console.log(已解析);
document.title=parsed.pagename+“|另一半| freddum.com”;
$(“#content”).html(parsed.content);
$(“#标题导航ul a:包含(“+页面+”).addClass(“选择导航”);
$(“body”).fadeIn();
}
函数myFailure(错误){
$(“#content').html('抱歉,出现错误:
'+错误); $(“body”).fadeIn(); } 功能加载页面(页面名称、成功、失败){ $.ajax({ url:“/backend/index.php/frontend/pull_page/”, 类型:“POST”, 数据:{ 页码:页码 }, 成功:一旦成功, 错误:onFailure }); } 加载页面(页面,mySuccess,myFailure); })();
在load_page函数的定义中没有“return”语句,至少不是直接的,因此通过执行
var json=load_page(page);
您将得到json=undefined。理想情况下,您应该稍微重新组织代码。有多种方法可以做到这一点,但这里有一种:

(function () {
    function mySuccess(json) {
        var parsed = $.parseJSON(json);
        console.log(json);
        console.log(parsed);
        document.title = parsed.pagename + " | The Other Half | freddum.com";
        $("#content").html(parsed.content);
        $("#header-navigation-ul a:Contains(" + page + ")").addClass("nav-selected");
        $("body").fadeIn();
    }
    function myFailure(error) {
        $('#content').html('Sorry, there was an error: <br>' + error);
        $("body").fadeIn();
    }
    function load_page(pagename, onSuccess, onFailure) {
        $.ajax({
            url: "/backend/index.php/frontend/pull_page/",
            type: "POST",
            data: {
                page: pagename
            },
            success: onSuccess,
            error: onFailure
        });
    }
    load_page(page, mySuccess, myFailure);
})();
(函数(){
函数mySuccess(json){
var parsed=$.parseJSON(json);
log(json);
console.log(已解析);
document.title=parsed.pagename+“|另一半| freddum.com”;
$(“#content”).html(parsed.content);
$(“#标题导航ul a:包含(“+页面+”).addClass(“选择导航”);
$(“body”).fadeIn();
}
函数myFailure(错误){