2

我尝试通过 ajax 使用 jquery 传递数组并返回,但我的代码运行不正常。我一直在改变它,但它不起作用。请告知该怎么做。我认为我的 jquery 代码还可以,但 php 代码不能重播。谢谢。

html代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.5/jquery.min.js"></script>

<script type="text/javascript" >
$(document).ready(function(){
    var allVals = { 
              'a': '1', 
              'b': '2',
              'c': '3' 
            }; 
            //$.each(allVals, function(key, value) { 
              //alert(key + ': ' + value); 
                //  });
            });
$.ajax({
    type: "POST",
     dataType: 'html',
     url: "test4.php",
     data: 'allVals=' + allVals,
         cache: false,
         success: function(data)
         {
            alert(data);
            $('#test').html(data);
            }
         });
</script>

<title>Insert title here</title>
</head>
<body>
<div id="test">##</div>
</body>
</html>

php代码test4.php

<?php
    $allVals = $_POST['allVals'];
    if ($allVals != ""){
          foreach ($allVals as $key => $value) {
          echo ($key.' '.$value."<br />");
    }     }

?>
4

5 回答 5

3

你也可以像这样从 jQuery 向 PHP 发送一个数组。在您的示例中,您发送一个对象:

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){
    header('Content-Type: text/html');
    print_r($_POST['allVals']);
    /*
    Array ( [0] => 1 [1] => 2 [2] => 3 ) 
    */
    die;
}
?>


<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function() {

    var myJavascriptArray = new Array('1', '2', '3');
    //or var myJavascriptArray=["1","2","3"]; 

    $.ajax({
        type: "POST",
        dataType: 'html',
        url: "test.php",
        data: {'allVals[]=' : myJavascriptArray},
        cache: false,
        success: function(data){
            $('#test').html(data);
        }
    });

});
</script>

</head>

<body>
<div id="test"></div>
</body>
</html>
于 2012-06-05T07:54:54.207 回答
1

您正在 $(document).ready(...-scope 之外进行 $.ajax 调用。将它移到里面!

我会做这部分

data: 'allVals=' + allVals

像这样:

data: allVals

如果您想像在 php 中一样接收它,请将其更改为:

data: {"allVals": allVals}

希望我的语法正确.. ;)

于 2012-06-05T07:43:21.247 回答
0
data: 'allVals=' + allVals,

这是罪魁祸首,发送的是allvals[object Object]

于 2012-06-05T07:41:30.477 回答
0

如果你需要一个 php 数组,我建议你这样做:

data: {allVals : allVals}

$_POST['allVals']遗嘱包含一个数组。

否则,如果您只需要单个参数,则必须执行以下操作:

var stringToSend = sep = "";
$.each(allVals, function(key, value) { 
    stringToSend += sep+key+"="+value;
    sep = "&";          
});
$.ajax({
type: "POST",
 dataType: 'html',
 url: "test4.php",
 data: stringToSend,
 ...
于 2012-06-05T07:48:02.050 回答
0

使用 jQuery $.POST方法会更好:

var javascriptArray = new Array('a', 'b', 'c');

$.post('url_to_test4.php', {'allVals[]': javascriptArray }, function(data){
   // perform some action with the response data.
});

这将向 PHP 文件发送一个名为 的数组allVals,其中包含您的javascriptArray.

于 2012-06-05T07:51:25.127 回答