使用Greasemonkey进行跨域帖子? [英] Cross-domain post with Greasemonkey?

查看:67
本文介绍了使用Greasemonkey进行跨域帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在后台发布Greasemonkey。我尝试动态创建iframe并发布到它,但它不起作用:

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



有人说即使我们发布,我们也是将无法接受这些价值观。如果我们不能,只需让用户访问该页面即可。它可以在JS,jQuery,AJAX帖子中。不仅仅是form-iframe技巧。


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.

推荐答案

Greasemonkey内置了对跨域发布的支持。您不需要使用jsonp,也不需要使用iframe。使用 GM_xmlhttpRequest函数

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

这篇关于使用Greasemonkey进行跨域帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆