I am firing a GET
request with Greasemonkey's GM_xmlhttpRequest()
:
$(".getReview").click(function(){
var videoId = $(this).parents("li").find("a").attr("href");
alert(videoId);
GM_xmlhttpRequest({
method: "GET",
url: "http://www.amitpatil.me/demos/ytube.php",
data: "username=johndoe&password=xyz123",
headers: {
"User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used.
"Accept": "text/xml" // If not specified, browser defaults will be used.
},
onload: function(response) {
console.log(response);
}
});
and here is the server code ytube.php:
<?php
print_r($_REQUEST);
print_r($_GET);
echo "Hello friends".$_GET['vid'];
?>
$_REQUEST
=> returns some data related to WordPress.
$_GET
=> returns a blank array.
I can't figure out what's wrong. I even tried the POST
method too.
The
data
parameter only works forPOST
methods. If you wish to send data with aGET
request, append it to the URL:But it's better to send data via
POST
, for a variety of reasons. To do that, you need to specify an encoding:If you are going to send a lot of data, or complex data, use JSON:
Your PHP would access it like so:
Update:
Greasemonkey and Tampermonkey now require that you set
@grant GM_xmlhttpRequest
in the metadata block. Be sure to do that.