我使用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控制台确认这一点。
答案 0 :(得分:0)
该网站使用的是jQuery v1.4.2,但使用done() was added in 1.5。
在<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js'></script>
<div class="container">
<div class="row">
<div class="col-12">
<h3>Checkboxes status to LocalStorage</h3>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck1">
<label class="custom-control-label" for="customCheck1">One</label>
</div>
</div>
<div class="col-12">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck2">
<label class="custom-control-label" for="customCheck2">Two</label>
</div>
</div>
<div class="col-12">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck3">
<label class="custom-control-label" for="customCheck3">Three</label>
</div>
</div>
<div class="col-12">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck4">
<label class="custom-control-label" for="customCheck4">Four</label>
</div>
</div>
<div class="col-12">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck5">
<label class="custom-control-label" for="customCheck5">Five</label>
</div>
</div>
<div class="col-12">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck6">
<label class="custom-control-label" for="customCheck6">Six</label>
</div>
</div>
<div class="col-12">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck7">
<label class="custom-control-label" for="customCheck7">Seven</label>
</div>
</div>
<div class="col-12">
<div class="form-group"></div>
<button id="clear" type="button" class="btn btn-danger">Clear LocalStorage</button>
</div>
</div>
</div>
中使用success
回调:
settings