【问题标题】:Postman-generated code won't send with Tampermonkey邮递员生成的代码不会与 Tampermonkey 一起发送
【发布时间】:2019-05-10 14:32:27
【问题描述】:

我使用 Postman 程序为我发送了一个 HTTP Post 请求。它适用于 Postman,这意味着它可以正确发送。

现在我尝试使用 Postman 的 generate code snippets 将请求的代码转换为 JavaScript jQuery AJAX 并将结果放入用户脚本中:

// ==UserScript==
// @name         Send HTTP
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://colorillo.com/
// @grant        none
// ==/UserScript==
var timerVar = setInterval (function() {DoMeEverySecond (); }, 5000); // << set to 2 seconds.

function DoMeEverySecond ()
{
var form = new FormData();
form.append("l", "[[158,115],[159,115],[159,116],[160,118],[161,119],[161,120],[162,121],[162,122]]");
form.append("w", "1");
form.append("c", "000000");
form.append("o", "100");
form.append("f", "1");
form.append("_", "false");

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://colorillo.com/draw.php?ing=_index",
  "method": "POST",
  "headers": {
    "Content-Type": "application/x-www-form-urlencoded",
    "X-Requested-With": "XMLHttpRequest",
    "Accept": "*/*",
    "User-Agent": "PostmanRuntime/7.11.0",
    "Cache-Control": "no-cache",
    "Postman-Token": "c6ecb194-94ba-4945-88fa-8d8cab3a1bd4,c20b3c71-cc61-4874-93c4-2f3c6b63d5b6",
    "Host": "colorillo.com",
    "cookie": "i=cloyfj1bio8uvxlqrkfkczqxo1pmx7m5; o=100; w=15; c=t44q; h=_-g00000____5aU00bH_GqCFXg3g_SY0gtx1J808RNApYLbO6g41X1wo____T000R01Puw3rMVU0t44q7w3F0afp4NcjXz00; a=lnpe0l; oi=qld8gxkfrzalpvqgydze7dzbm8p1r2zp",
    "accept-encoding": "gzip, deflate",
    "content-length": "753",
    "Connection": "keep-alive",
    "cache-control": "no-cache"
  },
  "processData": false,
  "contentType": false,
  "mimeType": "multipart/form-data",
  "data": form
}

$.ajax(settings).done(function (response) {
  console.log(response);
})();
}

在 TamperMonkey 中运行上述代码不会发送请求,并在控制台显示错误:

该站点使用 jQuery,我可以从 devtools 控制台确认。

【问题讨论】:

    标签: javascript jquery postman tampermonkey


    【解决方案1】:

    该站点使用的是 jQuery v1.4.2,但 done() was added in 1.5.

    settings 内使用success 回调:

    var settings = {
      "success": function (data) {
        console.log(data);
      },
      "async": true,
      "crossDomain": true,
      "url": "http://colorillo.com/draw.php?ing=_index",
      //.......................
    }
    
    $.ajax(settings);
    

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 2017-11-23
      • 2021-11-07
      • 1970-01-01
      • 2019-04-30
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多