下面的测试代码不对,怎么改
<!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;
?>
<!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;
?>
$data = json_decode($_POST);
$json = '{"test":"'.$data->test.'"}';
echo $json;
?>
$data = json_decode($_POST);
echo $data;
//$json = '{"test":"'.$data->test.'"}';
//echo $json;
?>
success : function(D, E) { //此处两个返回值吗?
alert(D);
//alert(D.test);
},
success: function(data, textStatus){
alert(data.test);
}如果不行,再次检查返回的json是否合乎规范。
没人认为传json过去,你也没仔细看,哈哈
<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; ?>"
}
我们现在也是这样的,如果楼主实在搞不清楚,建议把整个$_POST数组var_dump出来看一下就什么都清楚了,看看传过来的格式到底是数组还是json格式