简体   繁体   English

jQuery ajax无法正常工作

[英]jQuery ajax not working properly

I'm trying to implement some ajax function in my php website. 我正在尝试在我的php网站中实现一些ajax函数。 I basically know how to use jQuery, but for some reason, the returned value is always empty when I alert() it. 我基本上知道如何使用jQuery,但是由于某种原因,当我alert()时返回的值始终为空。 This is the code I use: 这是我使用的代码:

PHP: PHP:

if(_POST('ajax'))
{
    $ajax_action = _POST('ajax');
    if($ajax_action == "gwonline")
    {
        return 'test';
    }
}

JS / jQuery: JS / jQuery:

$.ajax({
    url: './include/ajax.php',
    data: {ajax: 'gwonline'},
    type: 'post',
    success: function(output) {
        alert(output);
    }
});

I debugged it and it does call the file with an ajax request and returns the value, but not received apparently.. What am I doing wrong here? 我对其进行了调试,它确实使用ajax请求调用了该文件并返回了该值,但显然没有收到该值。我在这里做错了什么? I sincerely don't know.. 我真不知道

The response should be pass by echo function, not return . 响应应该通过echo函数传递,而不是return So you must use echo 'test'; 因此,您必须使用echo 'test'; instead of return 'test'; 而不是return 'test';

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

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

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