简体   繁体   English

使用Ajax时500内部服务器错误

[英]500 internal server error when using ajax

I am receiving a 500 internal server error when making an ajax call. 进行Ajax调用时,我收到500个内部服务器错误。

Here is the javascript/jquery that makes the call: 这是进行呼叫的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);
        }
    });

And here is the php file being called: 这是被称为的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 returns nothing. xhr.responseText返回任何内容。

Is there an issue with my PHP code that is causing this error? 我的PHP代码是否存在导致此错误的问题?

Define your functions before using them, EG: 在使用它们之前,请先定义功能,例如:

function add($data) {
    return 'test';
}

if(isset($_POST['action']) && !empty($_POST['action'])) {
    $action = $_POST['action'];
    switch($action) {
        case 'add' : add($_POST);break;
    }
}

Using the keyword public is for Object oriented applications, if you have a setup like this: 如果您具有以下设置,则将关键字public用于面向对象的应用程序:

class Test_Class { 

    public function add($data) {
        return 'test';
    }

}

using the keyword public will be acceptable, but since you are not using objects for your function interaction, take away the keyword for a working set 使用关键字public是可以接受的,但是由于您不使用对象进行功能交互,因此请删除工作集中的关键字

Try changing public function to just function . 尝试将public function更改为正义function

function add($data) {
    return 'test';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM