pag*_*cio 1 javascript greasemonkey http-post gm-xmlhttprequest
我的脚本不起作用.AJAX调用没有发生.为什么?
// ==UserScript==
// @name prova
// @namespace http://blogpagliaccio.wordpress.com/
// @description prova
// @include http://*
// @version 1
// @grant GM_xmlhttpRequest
// @require http://userscripts.org/scripts/source/85398.user.js
// ==/UserScript==
// [........... other code]
console.log('start ajax call...');
GM_xmlhttpRequest({
method: "POST",
url: "www.prova.it",
data: {parametro:parametro},
onload: function(response) {
console.log(response.responseText);
},
onerror: function(reponse) {
alert('error');
console.log(reponse);
}
});
Run Code Online (Sandbox Code Playgroud)
我在一个@grant指令中列出了API函数,但是我没有看到AJAX调用和响应.
请参阅文档GM_xmlhttpRequest(). data只需要一个字符串.
如果您尝试发送非字符串数据data,您将收到如下错误:
组件没有请求的接口
(113超出范围67)
因此,您必须将数据编码为适当的字符串.此外,您需要发送适当的Content-Type标头.两种主要类型/方法是:
application/x-www-form-urlencodedapplication/json对于这两种方法,编码和发送数据看起来像这样:
表格编码数据:
GM_xmlhttpRequest ( {
method: "POST",
url: "www.prova.it",
data: "parametro=" + encodeURIComponent (parametro),
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function (response) {
console.log(response.responseText);
},
onerror: function(reponse) {
//alert('error');
console.log("error: ", reponse);
}
} );
Run Code Online (Sandbox Code Playgroud)
JSON序列化数据:
GM_xmlhttpRequest ( {
method: "POST",
url: "www.prova.it",
data: JSON.stringify ( {parametro:parametro} ),
headers: {
"Content-Type": "application/json"
},
onload: function (response) {
console.log(response.responseText);
},
onerror: function(reponse) {
//alert('error');
console.log("error: ", reponse);
}
} );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6143 次 |
| 最近记录: |