【发布时间】:2024-04-13 09:05:04
【问题描述】:
我在进行 ajax 调用时收到 500 内部服务器错误。
这是调用的 javascript/jquery:
$.ajax({url: 'ServicesAjaxHandler.php',
data: {action: 'add'},
type: 'post',
success: function(output) {
alert(output);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
alert(thrownError);
}
});
这里是被调用的 php 文件:
<?php
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'add' : add($_POST);break;
}
}
public function add($data) {
return 'test';
}
?>
xhr.responseText 不返回任何内容。
我的 PHP 代码是否存在导致此错误的问题?
【问题讨论】:
-
第 10 行有
public function。您不需要public部分。 -
@andrewsi 太好了,看起来这就是问题所在。一直在做很多面向对象的编程,最近迷茫了。谢谢。
标签: php javascript jquery ajax