【问题标题】:xmlhttprequest not being posted to php filexmlhttprequest 未发布到 php 文件
【发布时间】:2021-02-08 11:19:42
【问题描述】:

我正在尝试通过 javascript 向本地托管的 php 文件发出发布请求,但似乎 php 文件没有接收到数据或没有显示。 我尝试了各种使用 vardump print_r $_post $_request raw_http_data 打印的方法,但似乎没有任何效果。

代码有什么问题? 网址有问题吗?

这是 javascript 文件:

const rightswipe = (n) =>{
    var foo = "me";
    var bar = "ft";
    var xhr = new XMLHttpRequest();
    xhr.addEventListener("readystatechange", function() 
    {
        if(this.readyState === 4) 
        {
            if(this.status==200 || this.status==201)
            {
                //let newData = JSON.parse(this.responseText);
                //console.log(newData);
            }
            else{
                alert("invalid link");
            }
        }
    });
    xhr.open("POST", "like.php" , true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send("to_user="+foo+"&from_user="+bar);

};

like.php 是同一文件夹中的文件:

if (array_key_exists('foo', $_POST) && array_key_exists('bar', $_POST)) {
    
        $foo = $_POST['foo'];
        $bar = ($_POST['bar']);
        // do stuff with params
    
        echo 'Yes, it works!';
    
    } else {
        echo 'Invalid parameters!';
    }

$_post 和 $_request 也是空的; 和 var_dump($_server["REQUEST_METHOD"]) 返回 "GET" 我在本地使用 xampp 托管该站点,即 apache 和 mysql

【问题讨论】:

  • 你在浏览器开发者工具网络面板中查看过请求和响应了吗?
  • 也许rightswipe 永远不会被调用
  • @JaromandaX rightswipe 还包含其他 css 更改,因为它们无关紧要,所以我没有在此处包含它们。这些 CSS 更改正在发生,因此我可以向您保证,正在调用 rightwipe。
  • @DamianDziaduch 我在网络部分检查了它,请求方法显示 POST 并且没有给出任何错误
  • 错字:xhr.send("to_user="+foo+"&from_user="+bar); 您的密钥被称为 to_userfrom_user 而不是 foobar(您正在寻找的是:$foo = $_POST['foo'];

标签: javascript php apache xmlhttprequest http-post


【解决方案1】:

我已经设置了一个测试,您的代码运行良好。我只能看到一个问题,那就是您发布“to_user”和“from_user”参数,但在您的 PHP 中检查“foo”和“bar”键。因此,在我的测试中,我得到“无效参数!”按预期返回。

注意:目前您的 PHP 正在返回文本,因此如果您取消注释 JSON.parse 行,您可能会收到 JavaScript 错误。确保在 PHP 中对您的响应进行 json_encode。

我不确定显示请求方法的测试是“GET”,但怀疑它不是有效的测试,因为您清楚地发送了 POST 请求。

更新以下讨论:

我在 Windows 10 上安装了相同的 XAMPP 版本并设置了基本文件,但仍然无法复制问题。

C:\xampp\htdocs\rightswipe.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>

  <script src="rightswipe.js"></script>
</body>
</html>

C:\xampp\htdocs\rightswipe.js

const rightswipe = (n) =>{
    var foo = "me";
    var bar = "ft";
    var xhr = new XMLHttpRequest();
    xhr.addEventListener("readystatechange", function() 
    {
        if(this.readyState === 4) 
        {
            if(this.status==200 || this.status==201)
            {
                let newData = JSON.parse(this.responseText);
                console.log(newData);
            }
            else{
                alert("invalid link");
            }
        }
    });
    xhr.open("POST", "like.php" , true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send("to_user="+foo+"&from_user="+bar);

};

C:\xampp\htdocs\like.php

<?php
if (array_key_exists('to_user', $_POST) && array_key_exists('from_user', $_POST)) {
    
        $foo = $_POST['to_user'];
        $bar = ($_POST['from_user']);
        // do stuff with params
    
        echo json_encode('Yes, it works!');
    
    } else {
        echo json_encode('Invalid parameters!');
    }

在 Chrome 中访问 http://localhost/rightswipe.html 并打开开发者工具 (F12)

在控制台选项卡中,我输入了rightswipe(1); 并看到控制台日志消息“是的,它有效!”

转到网络选项卡以检查请求和响应:

一切都按预期工作。

【讨论】:

  • 谢谢@mark。我将它修复为 from_user 和 to_user 但它仍然不起作用;它仍然给出“无效的参数!” .此外,vardump $_POST 和 $_REQUEST 是空的。
  • 这告诉我,您的代码发生了其他事情,我们在这里看不到,因为正如我所说,我使用提供的代码设置了一个测试,一旦我纠正了这两个问题,它就可以正常工作提到的问题。 like.php 中是否有更多我们没有看到的代码?您将 vardump 放在哪里,您如何确认它们是空的?
  • 不,到目前为止,这都是 like.php 包含的。我暂时将 vardump 用于确认 $_post 为空。另外,当我尝试制作表格时,$_post 显示了结果
  • 当你测试这个时,你是点击一个按钮还是 HTML 上的一些其他用户操作,还是直接调用 rightwipe 函数?如果您正在使用单击等,请尝试直接调用该函数:打开开发者工具并在控制台选项卡中输入rightswipe(1);。在网络选项卡中找到请求并检查调用的完整 url 是否正确。检查请求负载以确保它已发送 to_user 和 from_user。您已经确认它是通过 POST 发送的。最后查看响应并共享响应有效负载。在执行此操作之前,请从 PHP 代码中删除所有转储。
  • 我正在点击一个按钮来调用 rightwipe。我试着像你说的那样通过右滑动来调用它,但它仍然不起作用。另外,我注意到在我发送请求的页面的网络面板中,其类型显示为 xhr,请求方法显示为 post。但是在接收页面上,类型是文档,请求方法是获取。我还在 php.ini 中将 enable_post_data_reading 从关闭更改为打开,但没有帮助。
猜你喜欢
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多