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

I need to post in the background with Greasemonkey. I tried to create an iframe dynamically and post to

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745648126a4638092.html

相关推荐

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

    I need to post in the background with Greasemonkey. I tried to create an iframe dynamically and post to

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信