本文介绍了从php数组中获取数据 - AJAX - jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个页面如下;
<head> <script type="text/javascript" src="jquery-1.6.1.js"></script> <script type="text/javascript"> $(document).ready( function() { $('#prev').click(function() { $.ajax({ type: 'POST', url: 'ajax.php', data: 'id=testdata', cache: false, success: function(result) { $('#content1').html(result[0]); }, }); }); }); </script> </head> <body> <table> <tr> <td id="prev">prev</td> <td id="content1">X</td> <td id="next">next</td> </tr> </table> </body>和一个php文件 ajax.php 到处理ajax请求为;
and a php file ajax.php to handle ajax requests as;
<?php $array = array(1,2,3,4,5,6); echo $array; ?>但是当我点击时,我得到 A 而不是数组[0]。我该如何解决这个问题?
But when I click, I am getting A instead of array[0]. How can I fix this??
提前致谢...
推荐答案你无法从js 尝试访问数组(php数组)
you cannot access array (php array) from js try
<?php $array = array(1,2,3,4,5,6); echo json_encode($array); ?>和js
$(document).ready( function() { $('#prev').click(function() { $.ajax({ type: 'POST', url: 'ajax.php', data: 'id=testdata', dataType: 'json', cache: false, success: function(result) { $('#content1').html(result[0]); }, }); }); });更多推荐
从php数组中获取数据
发布评论