Uncaught TypeError:无法读取由asinc调用引起的未定义属性'0'?

问题描述 投票:0回答:1

我收到此错误:无法读取未定义的属性'0'

我已经进行了一些研究,并且已经阅读到它可能是异步问题。

那是我的代码:json收到电话:accesWeb

我需要的JSON部分是tActivitats

[
  {
      "identificador":"1AE2886120F41BBC0F76567993EF76E0",
      "article":null,
      "precioTemp":[
         {
            "codActiv":"1",
            "codTarifa":"1",
            "precio":"8.50",
            "dtePromo":"0",
            "dteEuro":"%",
            "total":25.5,
            "tipus":"normal"
         }
      ],
      "tActivitats":[
         {
            "mostrIdioma":"S",
            "calendariWeb":"S",
            "nomActiv":"Visita Museu",
            "codiActiv":"1",
            "tipus":"S",
            "idioma":"0"
         }
      ]
   }
]

这是我的帖子:我想将tActivitats存储到名为result4的全局数组中。然后我调用一个名为loadSurvey的函数,并在其中传递该数组。

 function fConstTabla(dataVisita){
        //other stuff
        $.post('../ActionServletJson', {
                elem : 'accesWeb',
                codiMuseu: gCodiMuseu,
                property: gProperty,
                dataVisita: dataVisita,
                lang: "<%=lang%>",
                sessionId: "<%=sessionActual%>"
        }, function(data) {
            if(data.length>0){
                if(soloArticulo)borrarCarritoArticulo(numGrupActiv)
                else{
                    $.each(data,function(t,elem){
                       // other stuff//

                        if(elem.article=="S"){//article
                            $.each(elem.articulos,function(tArtic,elemArtic){
                                // other stuff//

                            });
                        }else{//actividad normal o combinada


                            result4.push(elem.tActivitats);

                            //other stuff

                        }
                    });

                    //other stuff//

                    var mostrar="<%=lang(propLang, "formdades.mostrarEnquesta")%>";
                    if(mostrar.toUpperCase()=="T")loadSurvey("<%=sessionActual%>",result4);

                    var avisActiv='<%=lang(propLang, "carrito.avisActivitat")%>';
                    fConstObservActiv(a_codiActiv.join(','),avisActiv);
                    $( "button" ).button().click(function( event ) {
                        event.preventDefault();
                    });
                }
            }else{
                //other stuff
            }
            hideLoading();
            mostrarPromocion(marcPromo);
        });

    }

然后,我有了loadSurvey函数(只有复制到出现错误的那一行为止)



function loadSurvey(sessionActual,result4){

    $.post('../ActionServletJson', {
                            elem : 'enquesta',
                            property: gProperty,
                            codiMuseu: gCodiMuseu,
                            lang: gLang,
                            sessionId: gSession,
                            date: new Date().getTime(),
                        }, function(data) {
        if(data.length>0 && data[0].totalPreguntes!="0"){

            //other stuff
            var count = 0;

            $.each(data,function(i,elem){


                if(auxNumGrupEnq!=elem.numGrup || auxNumElemEnq!=elem.numElem){//si hay varias linias, puede ser que alguna no tenga encuesta asignada
                    //other stuff

                    var nomactivitat =  fTrobarNom(result4[count],aActivitats,"","","activ");
                    count++
                    //other stuff

我在执行result4 [count]时遇到错误,它说:Uncaught TypeError:无法读取未定义的属性'0'

这是异步问题吗?我该如何解决?

javascript jquery arrays jsp post
1个回答
0
投票

[您可以使用return data存储从ajax调用中获得的结果,然后在任何地方使用它。我制作了示例代码供您理解流程,以便您可以据此更改代码。] >

这里是示例代码:

var arr = "";
//first this function will get call
function abc() {
  //calling function where ajax is present store return data in global variable
  arr = fConstTabla();
  loadSurvey(arr); //now call your other function 

}

function fConstTabla() {
  //other stuff
  $.post('url', {
    elem: 'something',

  }, function(data) {
    var result = data; //putting data got from ajax to some variable
  });
  return result; //return result to caling function


}

function loadSurvey(ss) {

  $.post('url', {
    elem: 'something',

  }, function(data) {
     console.log(ss);//this will also give same result
    console.log(arr); //print and see if data is still available
  });
}

(此代码已经过测试,可以正常工作)

所以,这是您可以在当前代码中进行的一些更改:

var result4 = [];
//first this functin will get call
function something(dataVisita) {
  result4 = fConstTabla(dataVisita); //getting return data
  var mostrar = "<%=lang(propLang, "
  formdades.mostrarEnquesta ")%>";
  if (mostrar.toUpperCase() == "T") loadSurvey("<%=sessionActual%>", result4); //passing to data to other function

}

function fConstTabla(dataVisita) {
  //other stuff
  $.post('../ActionServletJson', {
    elem: 'accesWeb',
    codiMuseu: gCodiMuseu,
    property: gProperty,
    dataVisita: dataVisita,
    lang: "<%=lang%>",
    sessionId: "<%=sessionActual%>"
  }, function(data) {
    if (data.length > 0) {
      if (soloArticulo) borrarCarritoArticulo(numGrupActiv)
      else {
        $.each(data, function(t, elem) {
          // other stuff//

          if (elem.article == "S") { //article
            $.each(elem.articulos, function(tArtic, elemArtic) {
              // other stuff//

            });
          } else { //actividad normal o combinada


            result4.push(elem.tActivitats);
          }
        });
        var avisActiv = '<%=lang(propLang, "carrito.avisActivitat")%>';
        fConstObservActiv(a_codiActiv.join(','), avisActiv);
        $("button").button().click(function(event) {
          event.preventDefault();
        });
      }
    } else {
      //other stuff
    }
    hideLoading();
    mostrarPromocion(marcPromo);
  });
  return result4; //return array to calling function
}

//other code
© www.soinside.com 2019 - 2024. All rights reserved.