这是我在index.html上的代码:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$.ajax({
type: "POST",
url: 'test.php',
data: "check",
success: function(data){
alert(data);
}
});
</script>
</head>
<body>
<div></div>
</body>
</html>
我如何编程test.php以获取在AJAX API中发送的“数据”?
您在这里问一个非常基本的问题。您首先应该阅读一些Ajax教程。只是为了帮助您(假设您了解发送数据的GET和POST方法),数据中的“数据”:函数(数据)中的“数据”与“数据”不同。为了清楚起见,您应该为它们命名不同,如下所示:
$.ajax({
type: "POST",
url: 'test.php',
data: "check",
success: function(response){
alert(response);
}
});
这清楚地表明,一个是要在POST参数中发送到test.php文件的数据,另一个是在运行后从test.php文件获得的响应。实际上,POST到test.php的数据参数必须像这里这样是一个哈希值(我在这里假设键为“类型”:
$.ajax({
type: "POST",
url: 'test.php',
data: {"type":"check"},
success: function(response){
alert(response);
}
});
显然,数据中可以有更多的键-值对。
因此,假设您的test.php文件是这样的:
if(isset($_POST['type'])){
//Do something
echo "The type you posted is ".$_POST['type'];
}
在这种情况下,您的警报应显示为:“您发布的类型为check”。这将根据您在AJAX调用中为“类型”键发送的值而改变。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句