json成功变量返回undefined,在console.log粘贴后有效

时间:2018-09-20 18:26:21

标签: json syntax

我将JSON成功存储在变量数据中。

console.log(data)显示如下:

{"day_name":"mon","h7":"h7","h8":"h8","h9":"h9"}

如果我尝试像这样day = data["day_name"];来使用它,它将以undefined的形式出现。

当我将console.log(data)直接复制到数据变量

var data = {"day_name":"mon","h7":"h7","h8":"h8","h9":"h9"};

然后再次运行它,效果很好。

我被困住了。 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

您的<?php use Restserver\Libraries\REST_Controller; defined('BASEPATH') OR exit('No direct script access allowed'); require APPPATH . 'libraries/REST_Controller.php'; require APPPATH . 'libraries/Format.php'; class Users extends REST_Controller { function __construct() { // Construct the parent class parent::__construct(); $this->load->model('user_model'); // Configure limits on our controller methods // Ensure you have created the 'limits' table and enabled 'limits' within application/config/rest.php $this->methods['users_get']['limit'] = 500; // 500 requests per hour per user/key $this->methods['users_post']['limit'] = 100; // 100 requests per hour per user/key $this->methods['users_delete']['limit'] = 50; // 50 requests per hour per user/key } /** * @method: GET * Fetch all users */ public function all_users_get() { $users = $this->user_model->all_users(); // Check if the users data store contains users (in case the database result returns NULL) if ($users) { // Set the response and exit $this->response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code } else { // Set the response and exit $this->response([ 'status' => FALSE, 'message' => 'No users were found' ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code } $this->response($users, REST_Controller::HTTP_OK); } /** * User Register * ************************* * * @param: fullname * @param: email address * @param: password * @param: username * * ************************* * @method: POST * @link : api/users/register */ public function resgister_post() { header('Access-Control-Allow-Origin: *'); $this->response("Teste", REST_Controller::HTTP_OK); } } 数据可能仍然是字符串。您是否曾经尝试过JSON?就像下面的示例一样:

JSON.parse(...)