Javascript Greasemonkey AJAX帖子似乎不起作用,即使指定了@grant

Javascript Greasemonkey AJAX帖子似乎不起作用,即使指定了@grant,javascript,http-post,greasemonkey,gm-xmlhttprequest,Javascript,Http Post,Greasemonkey,Gm Xmlhttprequest,我的脚本不起作用。AJAX调用没有发生。为什么? // ==UserScript== // @name prova // @namespace http://blogpagliaccio.wordpress.com/ // @description prova // @include http://* // @version 1 // @grant GM_xmlhttpRequest // @require http://userscript

我的脚本不起作用。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);
                    }
            });

我在
@grant
指令中列出了API函数,但没有看到AJAX调用和响应<代码>数据只接受一个字符串

如果尝试将非字符串数据发送到
数据
,则会出现如下错误:

组件没有请求的接口
(113超出范围67)

因此,您必须将数据编码为适当的字符串。此外,您还需要发送相应的
内容类型
标题。两种主要类型/方法是:

  • 应用程序/x-www-form-urlencoded

  • application/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);
        }
    } );
    
    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);
        }
    } );
    

    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);
        }
    } );
    
    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);
        }
    } );