用于GM_xmlhttpRequest()
允许跨域帖子(在大多数情况下都是如此)。
Greasemonkey 脚本:
// ==UserScript==
// @name _Sending arbitrary data, demo
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
var someArray = [1, 2, 3];
var serializedData = JSON.stringify (someArray);
GM_xmlhttpRequest ( {
method: "POST",
url: "http://SERVER.COM/PATH/ShowJSON_PostedData.php",
data: serializedData,
headers: {"Content-Type": "application/json"},
onload: function (response) {
console.log (response.responseText);
}
} );
ShowJSON_PostedData.php:
<?php
echo '<title>JSON data</title>';
echo '<h2>JSON post data:</h2><pre>';
$jsonData = json_decode($HTTP_RAW_POST_DATA);
print_r ($jsonData);
echo '</pre>';
?>
控制台。将会呈现:
<title>JSON data</title><h2>JSON post data:</h2><pre>Array
(
[0] => 1
[1] => 2
[2] => 3
)
</pre>