jQuery ajax不能正常工作

jQuery ajax not working properly

本文关键字:工作 常工作 ajax 不能 jQuery      更新时间:2023-09-26

我试图在我的php网站实现一些ajax功能。我基本上知道如何使用jQuery,但由于某种原因,返回值总是空的,当我alert()它。下面是我使用的代码:

PHP:

if(_POST('ajax'))
{
    $ajax_action = _POST('ajax');
    if($ajax_action == "gwonline")
    {
        return 'test';
    }
}
JS/jQuery:
$.ajax({
    url: './include/ajax.php',
    data: {ajax: 'gwonline'},
    type: 'post',
    success: function(output) {
        alert(output);
    }
});

我调试了它,它确实用ajax请求调用文件并返回值,但显然没有收到。我哪里做错了?我真的不知道……

响应应该由echo函数传递,而不是由return函数传递。所以你必须使用echo 'test';而不是return 'test';

不要忘记在php中每个变量前使用$符号