在AJAX中获取数据

时间:2016-01-16 16:26:18

标签: javascript jquery json ajax codeigniter

我使用短信功能创建APP,我想显示短信及其详细信息。我尝试过这样的事情,但这不起作用。 以下是SMS API的格式数据:

object(stdClass)#30 (1) {
  ["items"]=>
  array(2) {
    [0]=>
    object(stdClass)#31 (7) {
      ["id"]=>
      int(396629)
      ["type"]=>
      string(2) "nd"
      ["phone"]=>
      string(12) "123456789"
      ["recived"]=>
      string(19) "2016-01-16 16:36:42"
      ["blacklist"]=>
      bool(false)
      ["text"]=>
      string(19) "Vrs wifi nie dziala"
      ["contact"]=>
      object(stdClass)#32 (10) {
        ["id"]=>
        string(9) "272075197"
        ["first_name"]=>
        string(6) "FirstName"
        ["last_name"]=>
        string(5) "LastName"
        ["company"]=>
        string(0) ""
        ["phone"]=>
        string(12) "123456789"
        ["email"]=>
        string(0) ""
        ["tax_id"]=>
        string(0) ""
        ["city"]=>
        string(0) ""
        ["address"]=>
        string(0) ""
        ["description"]=>
        string(0) ""
      }
    }
    [1]=>
    object(stdClass)#33 (7) {
      ["id"]=>
      int(396609)
      ["type"]=>
      string(2) "nd"
      ["phone"]=>
      string(12) "123456789"
      ["recived"]=>
      string(19) "2016-01-16 14:36:53"
      ["blacklist"]=>
      bool(false)
      ["text"]=>
      string(3) "vrs"
      ["contact"]=>
      object(stdClass)#34 (10) {
        ["id"]=>
        string(9) "272075197"
        ["first_name"]=>
        string(6) "FirstName"
        ["last_name"]=>
        string(5) "LastName"
        ["company"]=>
        string(0) ""
        ["phone"]=>
        string(12) "123456789"
        ["email"]=>
        string(0) ""
        ["tax_id"]=>
        string(0) ""
        ["city"]=>
        string(0) ""
        ["address"]=>
        string(0) ""
        ["description"]=>
        string(0) ""
      }
    }
  }
}

我这样想:

$result = $serwersms->messages->recived('eco|nd|ndi|pre');

    $output = array();
    foreach ( $result->items as $key ) {
        $row = array();
        $row[] = $key->id;
        //var_dump RETURN: int(396609), etc.
        $row[] = $key->phone;
        $row[] = $key->text;
        $output[] = $row;

    }

    $output = array( "data" => $output );
    echo json_encode($output);

和AJAX:

$.ajax({
        url : "MY_URL_TO_FUNCTION",
        type: "GET",
        dataType: "JSON",
        success: function(data) {

            console.log(data.id);

        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('Error');
        }
});

但我没有获取数据,只是“未定义”。在控制台。 AJAX响应:

{"data":[[396629,"123456789","Vrs wifi nie dziala"],[396609,"123456789","vrs"]]}

有什么问题?

0 个答案:

没有答案