首页 > 解决方案 > 尝试在 Tampermonkey 中使用 GM_xmlhttpRequest 发布数据

问题描述

我正在尝试将数据从 tampermonkey 脚本发布到我的本地主机服务器。我已经编写了标准的 GM_xmlhttpRequest。我已经编写了所有必需的@grant 和@include。以下是 XHR 代码:

GM_xmlhttpRequest({
    method: "POST",
    url: "http://localhost:8080/user/index.php",
    data: "Hello World!",
    headers:  {
        "Content-Type": "application/x-www-form-urlencoded",
        "User-Agent": "Mozilla/5.0",
        "Accept": "text/xml"
    },
    onload: function(response){
        console.log(response.responseText);
    },
})

我正在获取 php 中的数据。我面临的问题是,每当我运行脚本并在 chrome 中检查它时,我都会在 doc 下看到我的 php 文件,而不是在网络选项卡中看到 XHR。以下是截图:

doc下的Index.php而不是XHR

以下是我的php代码:

<?php
$rawdata = file_get_contents("php://input");
var_dump($rawdata);
?>

这是我看到的输出

Hello string(0) ""

我不确定我错在哪里。

标签: javascriptphpxmlhttprequesttampermonkey

解决方案


推荐阅读