相关文章推荐
鼻子大的人字拖  ·  How to Prevent Out of ...·  1 月前    · 
闷骚的树叶  ·  How Fast is .NET ...·  1 月前    · 
另类的路灯  ·  Array.prototype.splice ...·  1 周前    · 
体贴的葡萄  ·  python - 使用带有 ...·  1 年前    · 
追风的匕首  ·  aws s3 cp folder ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams <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> <table> <td id="prev">prev</td> <td id="content1">X</td> <td id="next">next</td> </table> </body>

and a php file ajax.php to handle ajax requests as;

$array = array(1,2,3,4,5,6); echo $array;

But when I click, I am getting A instead of array[0]. How can I fix this?

$change = array('key1' => $var1, 'key2' => $var2, 'key3' => $var3); echo json_encode(change);

Then the jquery script ...

<script>
$.get("location.php", function(data){
var duce = jQuery.parseJSON(data);
var art1 = duce.key1;
var art2 = duce.key2;
var art3 = duce.key3;
</script>
                Just a note. jQuery.parseJSON is deprecated in latest versions of JQuery. Use JSON.parse instead.
– ASammour
                May 28, 2019 at 21:09
                spent hours trying to figure this out. My mistake was I had to parse the json THEN try to access the keys.
– 730wavy
                Oct 25, 2022 at 5:49

When you echo $array;, the result is Array, result[0] then represents the first character in Array which is A.

One way to handle this problem would be like this:

ajax.php

$array = array(1,2,3,4,5,6); foreach($array as $a) echo $a.",";

jquery code

$(function(){ /* short for $(document).ready(function(){ */
    $('#prev').click(function(){
        $.ajax({type:    'POST',
                 url:     'ajax.php',
                 data:    'id=testdata',
                 cache:   false,
                 success: function(data){
                     var tmp = data.split(",");
                     $('#content1').html(tmp[0]);

When you do echo $array;, PHP will simply echo 'Array' since it can't convert an array to a string. So The 'A' that you are actually getting is the first letter of Array, which is correct.

You might actually need

echo json_encode($array);

This should get you what you want.

EDIT : And obviously, you'd need to change your JS to work with JSON instead of just text (as pointed out by @genesis)

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.