Php Greasemonkey AJAX请求未发送数据?
我正在用Greasemonkey的Php Greasemonkey AJAX请求未发送数据?,php,ajax,json,greasemonkey,tampermonkey,Php,Ajax,Json,Greasemonkey,Tampermonkey,我正在用Greasemonkey的GM_xmlhttpRequest()发出一个GET请求: 下面是服务器代码ytube.php: <?php print_r($_REQUEST); print_r($_GET); echo "Hello friends".$_GET['vid']; ?> $\u请求=>返回一些与WordPress相关的数据。 $\u GET=>返回一个空白数组 我不知道怎么了。我甚至还尝试了POST方法 data参数仅适用于POST方法。如果希
GM_xmlhttpRequest()
发出一个GET
请求:
下面是服务器代码ytube.php:
<?php
print_r($_REQUEST);
print_r($_GET);
echo "Hello friends".$_GET['vid'];
?>
$\u请求
=>返回一些与WordPress相关的数据。
$\u GET
=>返回一个空白数组
我不知道怎么了。我甚至还尝试了
POST
方法 data参数仅适用于POST
方法。如果希望通过GET
请求发送数据,请将其附加到URL:
GM_xmlhttpRequest ( {
method: "GET",
url: "http://www.amitpatil.me/demos/ytube.php?username=johndoe&password=xyz123",
// Use no data: argument with a GET request.
... ...
} );
但出于各种原因,最好通过
POST
发送数据。为此,需要指定编码:
GM_xmlhttpRequest ( {
method: "POST",
url: "http://www.amitpatil.me/demos/ytube.php",
data: "username=johndoe&password=xyz123",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used.
"Accept": "text/xml" // If not specified, browser defaults will be used.
},
... ...
} );
如果要发送大量数据或复杂数据,请使用JSON:
var ajaxDataObj = {
u: username,
p: password,
vidInfo: [123, "LOLcats Terrorize City!", "Five stars"]
};
var serializedData = JSON.stringify (ajaxDataObj);
GM_xmlhttpRequest ( {
method: "POST",
url: "http://www.amitpatil.me/demos/ytube.php",
data: serializedData,
headers: {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used.
"Accept": "text/xml" // If not specified, browser defaults will be used.
},
... ...
} );
您的PHP将像这样访问它:
$jsonData = json_decode($HTTP_RAW_POST_DATA);
更新:
Greasemonkey和Tampermonkey现在要求您在元数据块中。一定要这样做
$jsonData = json_decode($HTTP_RAW_POST_DATA);