如何从本地php文件中的'$ .post'获取数据?

时间:2019-04-11 08:04:45

标签: javascript php ajax google-chrome-extension

我正在构建一个扩展程序,该扩展程序从网站(不是我的网站)中提取DOM,并自动完成一些输入的按钮单击。

我创建了一个本地数据库,该扩展名将从中提取值以填充输入,我可以使用xmlhttprequest成功地做到这一点,该xmlhttprequest从我的内容脚本js文件中读取我的php文件。

现在我要发送单击该按钮的php文件,以便它用新值更新数据库。我尝试了$ .post(),但无法通过$ _POST []获得它。

content-script.js

setTimeout(
      function(){
        var finsih_div_found = $(dialog_div_found).find("div").get(12);
        var finish_button_found = $(finsih_div_found).find("button").get(2);
        finish_button_found.dispatchEvent(new Event('click', {bubbles: true}))

        $.post('http://localhost:8012/extension-Oasis/php/getIntervenant.php', {button: 'Clicked'}, function(e){
            console.log("posted");
        });
},2000);

php文件

$status = $_POST["button"]; //Gives an error of 'Undefined index: button'.

请注意,该网站不是我所没有的,也不是后端,前端或它的API。我只想自动执行常规的按钮单击。

1 个答案:

答案 0 :(得分:0)

您是否直接调用过PHP文件?然后,这是一个HTTP GET请求,因此$ _POST ['button']引发并且出现“ Undefined index:button”错误。换句话说,只有使用jQuery $ .post才能通过$ _POST []访问按钮值。

   $.post('http://localhost:8012/extension-Oasis/php/getIntervenant.php', {button: 'Clicked'}, function(e){
        console.log(e); // log everything that is echoed in PHP file to browser's console
    });

在您的PHP文件中,在最后一行添加echo $status;