【问题标题】:Send and Recive an array with jQuery Ajax使用 jQuery Ajax 发送和接收数组
【发布时间】:2010-08-16 19:45:41
【问题描述】:

我想从 main.html 发送一个数组到 test.php 文件并从 test.php 接收另一个数组,但是它不起作用。请帮助我!

main.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Untitled Page</title>
    <script type="text/javascript" src="jquery/jquery-1.4.2.js"></script>
    <script type="text/javascript"><!--
        $(function () {
            $('#Button1').click(function () {
                var SendArrary = [];
                SendArrary[0] = $('#Text1').val();
                SendArrary[1] = $('#Text2').val();
                SendArrary[2] = $('#Text3').val();

                $.get('test.php',
                   SendArrary,
                      function (RecieveArray) {
                          $('#Text1').val(RecieveArray[0]);
                          $('#Text2').val(RecieveArray[1]);
                          $('#Text3').val(RecieveArray[2]);
                      },
                      alert("Ajax Done!!!")
                     )

            });



        });

    --></script>
</head>
<body>
    1<input id="Text1" type="text" /><br />
    2<input id="Text2" type="text" /><br />
    3<input id="Text3" type="text" /><br />
    <input id="Button1" type="button" value="button" />
</body>
</html>

test.php

<?php 


$RecieveArray = $_GET['SendArrary'];

$RecieveArray[0] = $RecieveArray[0]."1";
$RecieveArray[1] = $RecieveArray[1]."2";
$RecieveArray[2] = $RecieveArray[2]."3";


print_r($RecieveArray);

?>

【问题讨论】:

    标签: jquery ajax arrays


    【解决方案1】:

    懒惰的方式:

    jquery:http://api.jquery.com/jQuery.get/

    var callback = function(data){
         data = $.parseJSON(data);
         // do stuff with data
    }
    
    $.get("test.php", {
        array: SendArrary
    }, callback);
    

    php:http://www.php.net/manual/en/function.explode.php

    $arr = explode(",", $_GET['array']);
    

    注意:您的响应需要是有效的 JSON,例如:

    ["one", "two", "three", 4]
    

    用php:http://php.net/manual/en/function.implode.php

    echo("[".implode(",", $yourArray)."]");
    

    如果您想冒险超越发送和接收数组,您可能需要研究 php JSON 工具包。

    不确定 php 代码是否可以立即运行,我只是在手册中查找了它,可能这里或那里有错误,但如果你阅读了手册链接,你应该会开玩笑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多