【问题标题】:Send POST request from Chrome Extension从 Chrome 扩展发送 POST 请求
【发布时间】:2019-06-06 13:30:03
【问题描述】:

我正在编写一个 Chrome 扩展弹出窗口来登录我的服务器。该扩展有一个基本形式,包括usernamepassword 和一个submit 按钮。

<form>
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp"
           placeholder="Enter email">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
  </div>
  <button type="submit" class="btn btn-primary btn-sm" id="loginButton">Log In</button>
</form>

我使用 Insomnia REST 客户端测试了我的服务器响应:

网址:https://myserver.com/login
标头:Content-Type: application/x-www-form-urlencoded
表单 URL 编码:email: email@domain.com &amp; password: password

在我的 Chrome 扩展程序上,我编写了一个 signin.js 脚本来处理按钮单击事件并将请求发送到我的服务器。

// hardcoded for simplicity of this example
const email = email@domain.com
const pwd = password

var button = document.getElementById("loginButton");

button.addEventListener("click", function(){
    const req = new XMLHttpRequest();
    const baseUrl = "https://myserver.com/login";
    const urlParams = `email=${email}&password=${pwd}`;

    req.open("POST", baseUrl, true);
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.send(urlParams);

    req.onreadystatechange = function() { // Call a function when the state changes.
        if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
            console.log("Got response 200!");
        }
    }
});

然后在我的manifest.json 文件上,我拥有以下权限:

"permissions": [
    "storage",
    "activeTab",
    "cookies",
    "*://*.myserver.com/*"
  ],

扩展加载并运行没有错误,但我在 DevTools 的网络选项卡上看不到请求。我可以看到所有文件都已加载,但没有请求 myserver.com
请求的网址是Request URL: chrome-extension://ahlfehecmmmgbnpbfbokojepnogmajni/sign_in.html?

【问题讨论】:

  • 确保您查看的是correct devtools window
  • 哦,是的,我打开的是弹出式开发工具,而不是我当前的浏览器会话
  • 假设您点击了 chrome://extensions UI 页面中的重新加载图标,尝试调试您的代码:在点击监听器中设置断点并逐行运行。

标签: javascript ajax google-chrome-extension


【解决方案1】:

所以经过一番挖掘,我发现表单在点击提交按钮后会重新加载弹出窗口,因此,在我有机会看到请求之前它就已经刷新了。
作为解决方案,我必须通过如下编辑函数来禁用重新加载机制:

button.addEventListener("click", function(e){
    e.preventDefault();

    const req = new XMLHttpRequest();
    const baseUrl = "https://myserver.com/login";
    const urlParams = `email=${email}&password=${pwd}`;

    req.open("POST", baseUrl, true);
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.send(urlParams);

    req.onreadystatechange = function() { // Call a function when the state changes.
        if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
            console.log("Got response 200!");
        }
    }
});

现在它可以按预期工作了。

【讨论】:

  • 但是输出在哪里打印?哪个变量?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-30
  • 2012-06-20
  • 2014-02-09
  • 1970-01-01
  • 2016-09-26
  • 2014-09-26
相关资源
最近更新 更多