最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Cross-domain post with Greasemonkey? - Stack Overflow

matteradmin9PV0评论

I need to post in the background with Greasemonkey. I tried to create an iframe dynamically and post to it, but it didn't work:

function crossDomainPost() {
    // Add the iframe with a unique name
    var iframe = document.createElement("iframe");
    var uniqueString = "CHANGE_THIS_TO_SOME_UNIQUE_STRING";
    document.body.appendChild(iframe);
    iframe.style.display = "none";
    iframe.contentWindow.name = uniqueString;

    // construct a form with hidden inputs, targeting the iframe
    var form = document.createElement("form");
    form.target = uniqueString;
    form.action = "http://INSERT_YOUR_URL_HERE";
    form.method = "POST";

    // repeat for each parameter
    var input = document.createElement("input");
    input.type = "hidden";
    input.name = "INSERT_YOUR_PARAMETER_NAME_HERE";
    input.value = "INSERT_YOUR_PARAMETER_VALUE_HERE";
    form.appendChild(input);

    document.body.appendChild(form);
    form.submit();
}


Some people say even if we post, we won't be able take the values. if we can't, just making the user visit the page is enough. it can be in JS, jQuery, AJAX post. Not only the form-iframe trick.

I need to post in the background with Greasemonkey. I tried to create an iframe dynamically and post to it, but it didn't work:

function crossDomainPost() {
    // Add the iframe with a unique name
    var iframe = document.createElement("iframe");
    var uniqueString = "CHANGE_THIS_TO_SOME_UNIQUE_STRING";
    document.body.appendChild(iframe);
    iframe.style.display = "none";
    iframe.contentWindow.name = uniqueString;

    // construct a form with hidden inputs, targeting the iframe
    var form = document.createElement("form");
    form.target = uniqueString;
    form.action = "http://INSERT_YOUR_URL_HERE";
    form.method = "POST";

    // repeat for each parameter
    var input = document.createElement("input");
    input.type = "hidden";
    input.name = "INSERT_YOUR_PARAMETER_NAME_HERE";
    input.value = "INSERT_YOUR_PARAMETER_VALUE_HERE";
    form.appendChild(input);

    document.body.appendChild(form);
    form.submit();
}


Some people say even if we post, we won't be able take the values. if we can't, just making the user visit the page is enough. it can be in JS, jQuery, AJAX post. Not only the form-iframe trick.

Share Improve this question edited Jun 30, 2012 at 16:26 Brock Adams 93.7k23 gold badges241 silver badges305 bronze badges asked Jun 30, 2012 at 15:36 user1447165user1447165 871 gold badge2 silver badges8 bronze badges 3
  • Try searching cross-domain ajax; this is very, very, very mon problem. – Evan Davis Commented Jun 30, 2012 at 15:54
  • i searched and have just searched lots... all codes are about jsonp get, request... there is no post article. do you know a good source? – user1447165 Commented Jun 30, 2012 at 16:00
  • Yes, well, that's because you need to use jsonp. – Evan Davis Commented Jun 30, 2012 at 16:05
Add a ment  | 

1 Answer 1

Reset to default 5

Greasemonkey has built-in support for cross-domain posting. You do not need to use jsonp, nor an iframe. Use the GM_xmlhttpRequest function.

Rather than trying to build a form and post it, you send form-encoded data directly:

var formData1   = "1 INSERT_YOUR_PARAMETER_VALUE_HERE";
var formData2   = "2 INSERT_YOUR_PARAMETER_VALUE_HERE";
var formData3   = "3 INSERT_YOUR_PARAMETER_VALUE_HERE";
// etc.

GM_xmlhttpRequest ( {
    method:     "POST",
    url:        "http://YOUR_SERVER.COM/YOUR_PATH",
    data:       "formData1=" + encodeURIComponent (formData1)
                + "&" + "formData2=" + encodeURIComponent (formData2)
                + "&" + "formData3=" + encodeURIComponent (formData3)
                // etc.
                ,
    headers:    {
        "Content-Type": "application/x-www-form-urlencoded"
    },
    onload:     function (response) {
        console.log (response.responseText);
    }
} );
Post a comment

comment list (0)

  1. No comments so far