1.PHP
...
<script src="/jquery-1.3.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
var a = $a
var b = $b
var c = $c
apclick = function() {
$.ajax({
url: 'a1.php',
data: { a: a, b: b, c: c },
datatype: json,
success: function(results) {
if (results.msg == 'success') {
alert(a)
alert(b)
alert(c)
} else {
alert(results.msg)
}
},
error: function(results) {
alert("Data returned: " + results.msg )
}
});
setTimeout("location.reload(true);", 3000)
return false;
}
</script>
.....
<strong><br><a href="#" onclick="apclick();return false;">Afiseaza </a></strong>
a1.php
<?php
$return = array();
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c']
if ($a == "hello") {
$return['msg'] = 'success';
$return['a'] = "Buna";
};
if ($b == "say") {
$return['msg'] = 'success';
$return['a'] = "Spune";
};
if ($c == "man") {
$return['msg'] = 'success';
$return['a'] = "Om";
};
header("Content-type: application/json");
echo json_encode($a);
echo json_encode($b);
echo json_encode($c);
?>
问题是: 如何将a,b,c发送到a1.php并在1.php中接收a,b,c
答案 0 :(得分:0)
data: { 'a': 'a', 'b': 'b', 'c': 'c' },
type: 'POST'
尝试(使用数据上的引号和类型设置为POST。)
答案 1 :(得分:0)
提供的代码有几个语法错误,您应该在发布之前修复它们。
无论如何,这是适合您的工作代码:
<script type="text/javascript">
var a = "hello";
var b = "say";
var c = "man";
var res;
apclick = function() {
$.ajax({
url: 'a1.php',
data: { a: a, b: b, c: c },
datatype: 'json',
type: 'POST',
success: function(results) {
res = results;
if (results.msg == 'success') {
alert(results.a)
alert(results.a)
alert(results.a)
}
else {
alert(results.msg)
}
},
error: function(results) {
alert("Data returned: " + results.msg );
}
});
setTimeout("location.reload(true);",30000);
return false;
};
</script>
</head>
<body>
<strong>
<br>
<a href="#" onclick="apclick();return false;">Afiseaza </a>
</strong>
和a1.php:
<?php
$return = array();
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
if ($a == "hello") {
$return['msg'] = 'success';
$return['a'] = "Buna";
};
if ($b == "say") {
$return['msg'] = 'success';
$return['a'] = "Spune";
};
if ($c == "man") {
$return['msg'] = 'success';
$return['a'] = "Om";
};
header("Content-type: application/json");
echo json_encode($return);
?>
答案 2 :(得分:0)
为rsmoorthy竖起大拇指,但我不会使用$ _REQUEST
决定你的请求方法并将类型设置为Gazler相应的建议。