1

アーグ!! 私はこれを完璧に機能させましたが、今ではキーボードに頭をぶつけています。

配列内の定義された列にアクセスしたいのですが、未定義になっていますが、以下のコードの抜粋で詳しく説明されているように、アラートを使用して結果を表示すると、次のように表示されます。

[{"firstname":" Mr","0":" Mr","lastname":" Two","1":" Two","user_public_info_id":"3","2":"3","st_usage_id":null,"3":null},{"firstname":" Mr","0":" Mr","lastname":" Three","1":" Three","user_public_info_id":"5","2":"5","st_usage_id":null,"3":null}] 
*** 
g 
*** 
e 
*** 
undefined

Ajax コードは次のとおりです。

$.ajax({
 type: "POST",
 url: "models/ajaxHandler.php",
 data: "handler=getStActivitySharingList&stu_id="+stu_id,
 datatype: "json",
 success: function(result){
    var count = 0;
    if (result !== null)
    {
       //display results
       alert(result + " <br />*** <br />" + result[0] +" <br />*** <br />" + result[1] + " <br />*** <br />"  + result[0]["firstname"]);

       //clears choice list
       clearOptions();

       //result = $.parseJSON(result); //if this is used cannot display result again
       alert (result);   

       $.each(result, function (i, elem) {
           alert("elem"+elem.st_usage_id );    //displays as undefined and won't break
           if (elem.st_usage_id === null)
           {
               count++;
               alert(elem.firstname + " "+ elem.lastname + " " + elem.user_public_info_id);
               appendOption(elem);
           }
       });              
     }
     alert(count);
     if (count === 0){
         noResultsAvailableOption();
     }else{
        resultsAvailableOption();
      }

        ShowDialog(false);
        e.preventDefault();
      },
      error: function(){
           alert("ajax failure: could not retrieve a list of contacts");
      }
  });
4

2 に答える 2

0

PHPからどのように返すかわかりませんが、jqueryで試してください:

sucess: function (result)
{
    console.log(JSON.parse(result)); // parse string to json
}

json.orgを参照してください

于 2013-09-27T12:16:37.010 に答える
0

この質問に適切に答えるには、より適切なデバッグ手順を実装する必要があります。

この問題のデバッグに使用したコードは次のとおりです。xmlHttpRequest の分解により、データに問題があり、データを json にエンコードしようとしたときに不正な文字例外が発生したことが明確にわかりました。

問題を解決する優れた方法は、最初に正しいデバッグ手順を実装することです。

error: function(xmlHttpRequest, status, error){
    alert("ajax failure: could not populate list of countires | " + status + " | error:" + error);
    var xmlHttpRequestStr= "";
    for(x in xmlHttpRequest)
         xmlHttpRequestStr = xmlHttpRequestStr + xmlHttpRequest[x];
    alert(xmlHttpRequest);
}
于 2013-09-27T12:06:17.373 に答える