热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

没有从Ajax调用获取JSON数组

如何解决《没有从Ajax调用获取JSON数组》经验,为你挑选了1个好方法。



1> jeroen..:

您误解了Javascript和php的关系;该php仅在页面加载时呈现一次,因此您无法echo在Javascript success处理程序中返回php数据。

相反,您的php脚本输出的所有内容都将在Javascript变量中可用:

success: function(vnvlist) {
           //     ^^^^^^^ this is what has been outputted by php, the
           //             json already parsed

           // your data is available in `vnvlist`
           // var prevnvlist = '';

           // with dataType='json' you don't need to parse anything
           // as jQuery will parse it for you
           // var vnvlist = JSON.parse(prevnvlist);

           // so all you need is this...
           alert('success');
           for (var x = 1; x <= vnvlist.length; x++) {
             var vnv = vnvlist[x]['VnVMethod'];
             vnvSel.options[vnvSel.options.length] = new Option(vnv, vnv);
           }
         },

并且您需要确保php仅输出您的json字符串:

...
// The following line when read in the log file shows a valid JSON array
$log->lwrite('array '.json_encode($dataVnvMethods));

// output your data so that it is available on the client-side
echo json_encode($dataVnvMethods);

$log->lwrite('Ending debug');
...


推荐阅读
author-avatar
霸气的艳子_612
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有