【问题标题】:ajax success array bracket property does not return contentajax成功数组括号属性不返回内容
【发布时间】:2020-10-28 21:53:18
【问题描述】:

我遇到了一个我无法弄清楚的奇怪行为。我浏览了许多 S/O 文档,但没有找到解决方案。任何帮助/方向将不胜感激。谢谢。

本质上,我已经将两个数组嵌套到 Ajax 调用的返回中,并且我已经确认响应包含两个数组(在 ajax/php 文件中应用了 json 编码)。但是,当我尝试使用括号属性提取元素(例如,response[0]response[1])时,输出为空。

这是 Ajax 脚本和完整输出:

$.ajax({
            type: "POST",
            url: '/phpscripts/pass.php',  // 
            data: {row : row},
            success:(function(response){
                console.log("response = " + response);
            })
        });

response = [[{"id":"1","learn_load":"2","start_date":"2020-06-29","target_date":"2020-10-01","completed_date":"0000-00-00","bdl_20":"Establish a mentor relationship with Jerry Murray, Director of Finance to learn how to speak their lingo.","bdl_10":"","bdl_70":"Participate in a cross-organization committee to learn how supply chain initiatives reach across business units.","cg":"Business Acumen","cg_orn":"0","cg_tit":"1","cg_lj":"0"},{"id":"2","learn_load":"3","start_date":"2020-06-29","target_date":"2020-08-10","completed_date":"0000-00-00","bdl_20":"Establish mentor relationship with Sharon Yates, Director of Logistics to learn how to enable more automated and optimized business decisions.","bdl_10":"Complete Blue Yonder online certification course.","bdl_70":"Present learning updates to leadership team upon completion of formal training.","cg":"Machine Learning\/AI training","cg_orn":"1","cg_tit":"0","cg_lj":"0"},{"id":"3","learn_load":"1","start_date":"2020-06-29","target_date":"2020-09-01","completed_date":"0000-00-00","bdl_20":"Work with a coach to rehearse presentations and other tricky interactions with senior management.","bdl_10":"","bdl_70":"Seek out opportunities to interact informally with top management (e.g., receptions, charity events, off-sites, etc.) to enhance comfort level with them when back at the workplace.\r\n\r\n","cg":"Comfort Around Higher Management","cg_orn":"0","cg_tit":"1","cg_lj":"1"},{"id":"4","learn_load":"3","start_date":"2020-06-29","target_date":"2020-08-31","completed_date":"0000-00-00","bdl_20":"Work with a coach to shift mindset from tactical project management to a focus on conceptualizing large-picture projects and foreseeing challenges and needs to be addressed.","bdl_10":"","bdl_70":"Present to analysts throughout the SCM organization tactical vs strategic project planning and how to manage the successful application of both.","cg":"Strategic Project Management","cg_orn":"1","cg_tit":"1","cg_lj":"0"},{"id":"5","learn_load":"2","start_date":"2020-07-01","target_date":"2020-07-31","completed_date":"0000-00-00","bdl_20":"Competency Explicit Mentoring for macro design. Met with Jack Barrow.  ","bdl_10":"Attend Gateway Community College Excel #203 Intermediate Macro Design ","bdl_70":"Create 10 macro functions that could be used by team and provide overview at Wednesday meeting.","cg":"Excel Functions\/Macros","cg_orn":"1","cg_tit":"1","cg_lj":"0"}],[{"id":"1000001","cg":"Business Acumen","cg_orn":"0","cg_tit":"1","cg_lj":"0"},{"id":"1000002","cg":"Machine Learning\/AI training","cg_orn":"1","cg_tit":"0","cg_lj":"0"},{"id":"1000003","cg":"Comfort Around Higher Management","cg_orn":"0","cg_tit":"1","cg_lj":"1"},{"id":"1000004","cg":"Strategic Project Management","cg_orn":"1","cg_tit":"1","cg_lj":"0"},{"id":"1000005","cg":"Excel Functions\/Macros","cg_orn":"1","cg_tit":"1","cg_lj":"0"},{"id":"1000006","cg":"Written Communications (grammar\/structure)","cg_orn":"0","cg_tit":"1","cg_lj":"1"},{"id":"1000007","cg":"Leading a Team","cg_orn":"0","cg_tit":"1","cg_lj":"1"}]]

这是使用括号属性的 console.log 请求:

$.ajax({
            type: "POST",
            url: '/phpscripts/pass.php',  // 
            data: {row : row},
            success:(function(response){
                console.log("response0 = " + response[0]);
                console.log("response1 = " + response[1]);
            })
        });

我是新手,但我知道括号属性可以让我使用位置 0 和位置 1 隔离两个嵌套数组。我在这里缺少什么?

【问题讨论】:

  • var str = "[123]"; console.log(str[0]);

标签: php arrays ajax


【解决方案1】:

我敢打赌这是一个字符串化的 JSON。要解析它,只需执行以下操作:

response = JSON.parse(response);
console.log("response0 = ", response[0]);
console.log("response1 = ", response[1]);

【讨论】:

  • 我试了一下,但我从控制台得到的只是:response0 = [object Object],[object Object],[object Object],[object Object],[object Object] 仪表板。 php:2708 response1 = [对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象]
  • 查看我编辑的答案。原因是你不能简单地将字符串与对象连接起来
  • 这成功了。谢谢!我现在将回过头来看看正在做些什么来增加我的学习。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2011-02-14
  • 1970-01-01
  • 2014-07-07
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多