我想从PHP返回数组AJAX却是返回null而不是我派回AJAX为什么我的PHP在AJAX调用中返回NULL?
这里的数据是我的代码
的header.php
<?php
include_once("obtainData.php");
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>AJAX CALL</title>
<!-- JQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
的index.php
<?php
include_once("header.php");
?>
<div>
<h3>AJAX CALL</h3>
<p>
<?php
sendData();
?>
</p>
</div>
<?php
include_once("footer.php");
?>
footer.php
<script>
$.ajax({
url: "obtainData.php",
data: {action: 'sendData'},
type: 'GET',
success: function(data) {
alert(data);
alert("Success");
},
error: function() {
alert("Error.");
}
});
</script>
</body>
</html>
obtainData.php
<?php
if(isset($_POST["action"])){
$action = $_POST['action'];
switch($action) {
case 'sendData':
sendData();
break;
}
}
function sendData(){
$array = [];
for($i = 0; $i < 20; $i++){
$array[$i] = "prove";
}
echo json_encode($array);
}
?>
如果我打电话给我的sendData
方法在我的嵌入式PHP它显示我的输出,但我无法从AJAX得到它呼叫。
我错过了什么吗?
提前致谢!
变化GET或POST的好 – Raman