下面的测试代码不对,怎么改
<!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=UTF-8">
<script src='scripts/jquery-1.4.2.min.js' type='text/javascript'></script>
<title>Insert title here</title>
</head>
<body>
<script>
$.ajax({
type : "post",
url : "localhost/test/test_ajax.php",
dataType : "json",
data : {
test : "1"
},
success : function(D, E) {
alert(D.test);
},
error: function(C, D, E) {
alert('error');
}
})
</script>
</body>
</html>
test.php<?php
$data = json_decode($_POST);
$json = '{"test":"'+$_POST->test+'"}';
echo $json;
?>

解决方案 »

  1.   

    php文件名没错,发帖的时候的笔误
      

  2.   

    php 能用+连接吗?用.连接
      

  3.   

    php改了,还是不行<?php
    $data = json_decode($_POST);
    $json = '{"test":"'.$data->test.'"}';
    echo $json;
    ?>
      

  4.   

    你可以先把后面的注释掉。看$data能在前台弹出吗? <?php
    $data = json_decode($_POST);
    echo $data;
    //$json = '{"test":"'.$data->test.'"}';
    //echo $json;
    ?>
     success : function(D, E) {   //此处两个返回值吗? 
                   alert(D);  
                  //alert(D.test);
                },
      

  5.   

    先把dataType : "json",注释掉,然后看能不能弹出。如果可以,这里的参数名称不要改动
    success: function(data, textStatus){
        alert(data.test);
    }如果不行,再次检查返回的json是否合乎规范。
      

  6.   

    有提示说json_decode()的参数需要是string
      

  7.   

    $data = json_decode($_POST);POST过来的是什么?你没写。$_POST['xxxx']
      

  8.   

    看1楼的html代码,我仿照一个商业网站客户端的写法
      

  9.   

    json_decode 对字符串进行编码.你对个数组进行编码。你想他怎么运作?
      

  10.   

    不管你怎么写 PHP有他的规范你就应该遵循。要不就自己写函数 是不是。
      

  11.   

    本帖最后由 xuzuning 于 2011-09-21 07:07:23 编辑
      

  12.   


    没人认为传json过去,你也没仔细看,哈哈
      

  13.   

    1.首先你要了解,ajax传送数据  data:{ test : "1" , test2: "2" } 这个数据后台会得到什么。它其实是跟POST方式一样的,与下面方式一样
    <form>
        <input type="text" name="test" value="1" />
        <input type="text" name="test2" value="2" />
    </form>2. dataType : "json" 表示,后台应该返回一段数据,这段数据应符合JSON的格式将你的后台PHP代码改成下面的代码就可以通过测试
    <?php
    $testValue=$_POST["test"];  //这是data :{ test:"1" } 传过来的变量
    ?>
    {
        "test":"<?php echo "the value of test is :".$testValue;  ?>"
    }
      

  14.   


    我们现在也是这样的,如果楼主实在搞不清楚,建议把整个$_POST数组var_dump出来看一下就什么都清楚了,看看传过来的格式到底是数组还是json格式