Javascript 从 php 数组中获取数据 - AJAX - jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6395720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:32:12  来源:igfitidea点击:

Get data from php array - AJAX - jQuery

javascriptjsonjquery

提问by blasteralfred Ψ

I have a page as below;

我有一个页面如下;

<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>

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

和一个 php 文件ajax.php来处理 ajax 请求;

<?php
$array = array(1,2,3,4,5,6);
echo $array;
?>

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

但是当我点击时,我得到的A不是数组 [0]。我怎样才能解决这个问题??

Thanks in advance...

提前致谢...

回答by genesis

you cannot access array (php array) from js try

你不能从js访问数组(php数组)试试

<?php
$array = array(1,2,3,4,5,6);
echo json_encode($array);
?>

and js

和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]);
            },
        });
    });
});

回答by ballsDeep

quite possibly the simplest method ...

很可能是最简单的方法......

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

Then the jquery script ...

然后jquery脚本...

<script>
$.get("location.php", function(data){
var duce = jQuery.parseJSON(data);
var art1 = duce.key1;
var art2 = duce.key2;
var art3 = duce.key3;
});
</script>

回答by Kokos

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

当你echo $array;,结果是Arrayresult[0]那么代表其中的第一个字符ArrayA

One way to handle this problem would be like this:

处理这个问题的一种方法是这样的:

ajax.php

ajax.php

<?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]);
                 }
                });
    });

});

回答by Jagan Chennai

you cannot access array (php array) from js try

你不能从js访问数组(php数组)试试

<?php
$array = array(1,2,3,4,5,6);
echo implode('~',$array);
?>

and js

和js

$(document).ready( function() {
$('#prev').click(function() {
  $.ajax({
  type: 'POST',
  url: 'ajax.php',
  data: 'id=testdata',
  cache: false,
  success: function(data) {
    result=data.split('~');
    $('#content1').html(result[0]);
  },
  });
});
});

回答by JohnP

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.

当您这样做时echo $array;,PHP 将简单地回显 'Array',因为它无法将数组转换为字符串。所以你实际得到的'A'是Array的第一个字母,这是正确的。

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)

编辑:显然,您需要更改您的 JS 以使用 JSON 而不仅仅是文本(如@genesis 所指出的)