1

Arg!! I had this working flawlessly and now I am back to banging head against the keyboard.

I want access defined columns inside the array, but I am getting undefined but if I display the results using an alert as detailed in snipped of code below I see the following:

[{"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

Here is the Ajax code:

$.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

更好地回答这个问题是实施更好的调试程序。

这是我用于调试此问题的代码。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 回答
0

我不知道你如何从 PHP 中返回它,但在 jquery 中尝试:

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

json.org

于 2013-09-27T12:16:37.010 回答