对象响应ajax返回为undefined

时间:2015-10-21 20:26:50

标签: javascript ajax

这个问题实际上让我发疯了。实际上排除了一个ajax请求的耗尽值,到目前为止,他们的方法非常好,所有字段都正确地返回了php json,并且还指出了:unavailable。这个字段没有被类看到,我得到答案ajax返回undefined,也许用我可以更好地解释的代码:

Ajax请求

$.post(postUrl, postData, function(response) 
    {
        console.log(response);
        console.log(response.unavailables);
        ...

console.log中的响应结果

enter image description here

和console.log中的response.unavailables

enter image description here

如何在响应中看到我正确地unavailables array,但代码没有看到这一点,我已经检查了php类几个小时,检查结果和其他所有东西正确!事实上,返回值不可用,但由于一个奇怪的原因,我得到一个未定义的。为什么会这样?

Matteo解决方案

$.each(response, function(_, obj) 
                        {
                            $.each(obj, function(key, val) 
                            {
                                if (key === 'unavailables') 
                                {
                                    console.log("val => " , val);
                                }
                            });
                        });

enter image description here

更多详情:

enter image description here

2 个答案:

答案 0 :(得分:3)

response[0].unavailables

如果您在控制台日志中查看数据结构,事实上,您会发现unavailablesappointments处于同一级别,这位于密钥0内,因此{ {1}}也是。

编辑:我认为循环所有unavailables的唯一方法如下:

unavailables

答案 1 :(得分:2)

因此您可以使用...

访问该项目
.name,
.center,
.phone {
  font-size: 14px;
  color: green;
  margin-bottom: 15px;
  display: inline-block;
}
.name {
  width: 55%
}
append .center,
.phone {
  width: 20%;
}
.sort-by {
  color: red;
}
strong,
.not-matched {
  display: none;
}
.matched {
  display: block;
}

我是指数。

例如......

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="plate">
  <form action="#">
    <input type="text" id="sort-plate" />


  </form>
  <br />
  <br />
  <div>
    <div class="sort-plate">
      <div class="name"> <strong>Name</strong> Chibueze Okechukwu</div>
      <div class="center"> <strong>Department</strong> Act Center</div>
      <div class="phone"> <strong>Ext</strong> 5204</div>
    </div>
    <div class="sort-plate">
      <div class="name"> <strong>Name</strong> Angelina Jolie</div>
      <div class="center"> <strong>Department</strong> Act Center</div>
      <div class="phone"> <strong>Ext</strong> 5204</div>
    </div>
    <div class="sort-plate">
      <div class="name"> <strong>Name</strong> Michael Jordan</div>
      <div class="center"> <strong>Department</strong> Act Center</div>
      <div class="phone"> <strong>Ext</strong> 5204</div>
    </div>
    <div class="sort-plate">
      <div class="name"> <strong>Name</strong> Deka Junior</div>
      <div class="center"> <strong>Department</strong> Act Center</div>
      <div class="phone"> <strong>Ext</strong> 5204</div>
    </div>
    <div class="sort-plate">
      <div class="name"> <strong>Name</strong> Chris Okorondu</div>
      <div class="center"> <strong>Department</strong> Act Center</div>
      <div class="phone"> <strong>Ext</strong> 5204</div>
    </div>
    <div class="sort-plate">
      <div class="name"> <strong>Name</strong> Angela Jones</div>
      <div class="center"> <strong>Department</strong> Act Center</div>
      <div class="phone"> <strong>Ext</strong> 5204</div>
    </div>
    <div class="sort-plate">
      <div class="name"> <strong>Name</strong> Ikechukwu Adaora</div>
      <div class="center"> <strong>Department</strong> Act Center</div>
      <div class="phone"> <strong>Ext</strong> 5204</div>
    </div>

    <div class="sort-plate">
      <div class="name"> <strong>Name</strong> Chris Okorondu</div>
      <div class="center"> <strong>Department</strong> Act Center</div>
      <div class="phone"> <strong>Ext</strong> 5204</div>
    </div>

    <div class="sort-plate">
      <div class="name"> <strong>Name</strong> Chris Ndolo</div>
      <div class="center"> <strong>Department</strong> Act Center</div>
      <div class="phone"> <strong>Ext</strong> 5204</div>
    </div>

    <div class="sort-plate">
      <div class="name"> <strong>Name</strong> Chris Stones</div>
      <div class="center"> <strong>Department</strong> Act Center</div>
      <div class="phone"> <strong>Ext</strong> 5204</div>
    </div>

    <div class="sort-plate">
      <div class="name"> <strong>Name</strong> Chris Johnson</div>
      <div class="center"> <strong>Department</strong> Act Center</div>
      <div class="phone"> <strong>Ext</strong> 5204</div>
    </div>
  </div>