简体   繁体   English

检测是否已发送Ajax请求

[英]Detect if Ajax request has been sent

I just cannot figure out how to check in PHP if an ajax request has been sent (form submission). 我只是不知道如何检查是否已发送ajax请求(表单提交)的PHP。

neither 都不

if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')

nor 也不

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')

gets executed. 被执行。

Here is the javascript code. 这是JavaScript代码。

$.ajax({
       type: "POST",
       url: 'login.php',
       data: $(this).serialize()
   });

"this" is referring to the form. “ this”是指表格。 I have checked it and it is in the correct format username=dean&password=123123 我已经检查了它,并且它采用了正确的格式username = dean&password = 123123

What am I doing wrong? 我究竟做错了什么?

Add a callback... 添加回调...

  $.ajax({
       type: "POST",
       url: 'login.php',
       data: $(this).serialize(),
       success : function(response){
             console.log(response);
        }
   });

Try to use browser developer tool (FireBug Network panel if you're using Mozilla Firefox), to make sure that the appropriate header is sent. 尝试使用浏览器开发人员工具(如果使用的是Mozilla Firefox,则为FireBug网络面板),以确保发送了适当的标头。 And which version of jQuery are you using? 您正在使用哪个版本的jQuery? Also, if you are using PHP-FPM as backend for Nginx http server, Nginx may not send some HTTP-headers to backend, depending on it's configuration. 另外,如果您将PHP-FPM用作Nginx http服务器的后端,则Nginx可能不会将某些HTTP标头发送到后端,具体取决于其配置。

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

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