weixin_33688840 2012-12-17 15:49 采纳率: 0%
浏览 22

在jQuery中解析JSON数组

I have a php function that returns json data that looks like this:

    if ($result !== false) 
    {
        $resultdata = array(
        'status' => true,
        'error_msg'=>'',
        'data' => $result
        );
    }
    else 
    {
        $resultdata = array(
        'status' => false,
        'error_msg'=>"Unable to reassign port vlan.  Check Port mode.",
        'data' => $result
        );

    }       
    header ('Content-Type: application/json; charset=UTF-8');
    echo json_encode($resultdata);

The data from the model ($result) is built like this:

    $pattern = "/([0-9]+)(\s+)([0-9]+)(\s+)([a-z]+)(\s+)([a-z]+)(\s+)/i";
    if (preg_match($pattern, $this->_data, $matches)) 
    {
      $vlandetailsArray['VlanId']= $matches[1];
      $vlandetailsArray['Name']= $matches[3];
      $vlandetailsArray['Egress']= $matches[5];
      $vlandetailsArray['Type']= $matches[7];
    }

And the code in my view looks like this:

        $.ajax({
                  url:fullpath,
                  type:'POST',
                  dataType:'json',
                  success: function(returnDataFromController) {
                    console.log(returnDataFromController);
                    if (returnDataFromController.status == true)
                    {
                      //build table contents 
                      var htmlstring = '<thead>';  
                      htmlstring = htmlstring + '<tr>';
                      htmlstring = htmlstring + '<th>VlanId</th>';
                      htmlstring = htmlstring + '<th>Name</th>';
                      htmlstring = htmlstring + '<th>Egress Rule</th>';
                      htmlstring = htmlstring + '<th>Port Membership Type</th>';
                      htmlstring = htmlstring + '<tbody>';
                      //should only be one entry so no need to loop

                     // htmlstring = htmlstring + "<tr>"
                      //htmlstring = htmlstring + "<td class ='vlanid'>" + returnDataFromController.VlanId + "</td>";
                      //htmlstring = htmlstring + "<td>" + returnDataFromController.Name + "</td>";
                      //htmlstring = htmlstring + "<td>" + returnDataFromController.Egress + "</td>";
                     // htmlstring = htmlstring + "<td>" + returnDataFromController.Type +"</td>";
                      //htmlstring = htmlstring + "</tr>";      

                      $('#assignedvlans').html(htmlstring);
                      $('#progress-indicator').hide();

                      }

                      else

                      {
                                  alert('my error message: ' + returnDataFromController.error_msg);
                      }
                                },//end success
                error:  function(jqXHR, textStatus, errorThrown)
                {
                          alert(errorThrown);
                }

What I'm wondering is how to access the "data" section of my return array, and the "error_msg". Right now, after the ajax is executed, when the status is false, the alert that appears shows "my error message:undefined". But I'm not getting any error messages about "returnDataFromController.status"

As far as the data is concerned, I've tried :

 returnDataFromController.data.VlanId

and

 returnDataFromController["#data"].VlanId

but neither one seems to work. The code that's been commented out used to work when i wasn't returning a wrapper array around the data . i was previously just returning "$result" from the controller...

Any suggestions would be appreciated. Thanks.

UPDATE 1 By the way, the console.log doesn't really help me much. For some reason, in the web console, i don't see any data being dumped there. I'm using firefox...

UPDATE 2

When the controller returns true for the status, this is the error message I get from the ajax error handler:

SyntaxError: JSON.parse: unexpected character

I have changed my code to use:

 returnDataFromController.data.VlanId

UPDATE 3

To simplify matters, I tried to recreate my problems using more basic code. To that end, here's my controller:

public function test()
{
    $mydata = array(
      'VlanID'=>80,
      'Name'=>'test vlan'
    );
   $data = array(
      'status'=> true,
      'error_msg'=>'all is well',
      'data'=>$mydata
   );
    header ('Content-Type: application/json; charset=UTF-8');
    echo json_encode($data);

}

And here's my view:

    $('#testbutton').click(function() {             
            $.ajax({                  
                url:'http://myserver/myapp/index.php/controller/test/',
                type:'POST',
                dataType:'json',
                success: function(returnDataFromController) {   
                 alert('success');
                 console.log(returnDataFromController);
                                    alert(returnDataFromController.data.VlanID);
                  },//end success
                error:  function(jqXHR, textStatus, errorThrown)
                {
                     alert('ajax error handler: ' + errorThrown);
                }                     
              }); //end ajax call   

When i click on the test button, this is what happens: 1. i get the first alert that says "success". 2. nothing is written to the console 3. i get the following error message in the console - when trying to do the second alert "TypeError: returnDataFromController.data is undefined".

hope this example simplifies things and helps to id where I'm going wrong. thanks.

UPDATE 4

When I add:

   alert(returnDataFromController.toSource());

I get the following output:

      ({VlanID:80, Name:"test vlan"})
  • 写回答

1条回答 默认 最新

  • weixin_33697898 2012-12-17 18:10
    关注

    Try this:

       $('#testbutton').click(function() {             
            $.ajax({                  
                url:'http://myserver/myapp/index.php/controller/test/',
                type:'POST',
                dataType:'json',
                success: function(returnDataFromController) {   
                 alert('success');
                 console.log(returnDataFromController);
                alert(returnDataFromController.VlanID);
                  },//end success
                error:  function(jqXHR, textStatus, errorThrown)
                {
                     alert('ajax error handler: ' + errorThrown);
                }                     
              }); //end ajax call  
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题