使用Greasemonkey进行跨域帖子?

编程入门 行业动态 更新时间:2024-10-11 13:23:55
本文介绍了使用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 = "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: "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进行跨域帖子?

本文发布于:2023-11-28 12:43:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:帖子   Greasemonkey

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!