php – Greasemonkey AJAX请求不发送数据?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – Greasemonkey AJAX请求不发送数据?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2137字,纯文字阅读大概需要4分钟。
内容图文
![php – Greasemonkey AJAX请求不发送数据?](/upload/InfoBanner/zyjiaocheng/799/589f79d3abaf4f9889986c554d7da9e7.jpg)
我正在用Greasemonkey的GM_xmlhttpRequest()发出一个GET请求:
$(".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.
},
onl oad: function(response) {
console.log(response);
}
});
这是服务器代码ytube.php:
<?php
print_r($_REQUEST);
print_r($_GET);
echo "Hello friends".$_GET['vid'];
?>
$_REQUEST =>返回一些与WordPress相关的数据.
$_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现在要求您在元数据块中使用set @grant GM_xmlhttpRequest.一定要这样做.
内容总结
以上是互联网集市为您收集整理的php – Greasemonkey AJAX请求不发送数据?全部内容,希望文章能够帮你解决php – Greasemonkey AJAX请求不发送数据?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。