无法访问成功ajax中的元素

时间:2013-04-26 12:13:01

标签: javascript jquery ajax

我在ajax调用中调用了一个ajax调用,但是在第二个调用中,该元素未被识别,例如:

$.ajax({
    url: 'controleFatAcoes.php',
    type: 'post',
    dataType: 'html',
    data: {
        acao: 'validaenviofat',
        id_cliente: cli.id_cliente,
        dt_fat: cli.id_fat
    },
    success: function(data)  {
        $.ajax({
            url: 'controleFatAcoes.php',
            data: {id_cliente: cli.id_cliente, 
                  id_fat: cli.id_fat, acao: 'getdadosnf'},
            type: 'post',
            dataType: 'json',
            success: function(dados) {
                    **$('#templateEmpresa').html(dados.empresa);**
            }
        )};
});

当我运行console.log($('#templateEmpresa'))时,我得到:

[context: document, selector: "#templateEmpresa", constructor: function, init: function, selector: ""…]

1 个答案:

答案 0 :(得分:0)

尝试使用此代码: 鉴于您正在第一个ajax调用的success函数内执行第二个ajax调用,可能是范围问题。

var firstajaxsuccess = 0;
$.ajax({
    url: 'controleFatAcoes.php',
    type: 'post',
    dataType: 'html',
    data: {
        acao: 'validaenviofat',
        id_cliente: cli.id_cliente,
        dt_fat: cli.id_fat
    },
    success: function(data)  {
         firstajaxsuccess = 1;
    }
});
if(firstajaxsuccess){
        $.ajax({
            url: 'controleFatAcoes.php',
            data: {id_cliente: cli.id_cliente, 
                  id_fat: cli.id_fat, acao: 'getdadosnf'},
            type: 'post',
            dataType: 'json',
            success: function(dados) {
                    **$('#templateEmpresa').html(dados.empresa);**
            }
        )};
}
相关问题