简体   繁体   English

如何阻止form.submit()将我重定向到另一个页面?

[英]How can I stop a form.submit() from redirecting me to another page?

I'm building a Web App and I am required to make some form of Cross-Domain POST request to a server which I don't personally have access to. 我正在构建一个Web应用程序,并且我需要对我个人没有访问权限的服务器发出某种形式的跨域POST请求。

I've tried many things to make it work with Ajax but I found no success. 我已经尝试了很多方法来使其与Ajax一起使用,但没有成功。 I did a bit of research and eventually I eventually found a solution which was to create an invisible iframe with a form, then submit the said form to get back the information I require. 我做了一些研究,最终找到了一个解决方案,该方案是使用表单创建一个不可见的iframe,然后提交所述表单以获取我需要的信息。

However, this form is created in javascript, so I use the form.submit() method to submit the said form, and this, rather than simply giving me the JSON file with the information I require, redirects me to the actual JSON file in a different page altogether. 但是,此表单是用javascript创建的,因此我使用form.submit()方法提交了所述表单,这不仅是将所需信息提供给我JSON文件,而且还将我重定向到了其中的实际JSON文件。完全不同的页面。

How can I prevent this from happening? 如何防止这种情况发生?

    var ifr = document.createElement('iframe');
    var frm = document.createElement('form');
    frm.setAttribute("id", "invis_form");
    frm.setAttribute("action", endpoint);
    frm.setAttribute("method", "post");

    var x = document.createElement("INPUT");
    x.setAttribute("type", "text");
    x.setAttribute("name", "chave");
    x.setAttribute("value", apikey);

    var y = document.createElement("INPUT");
    y.setAttribute("type", "text");
    y.setAttribute("name", "valor");
    y.setAttribute("value", "125");

    frm.appendChild(x);
    frm.appendChild(y);

    ifr.appendChild(frm);
    document.body.appendChild(ifr);
    frm.submit();

I was expecting to get something I could print on the console, not to be redirected to a JSON file with the information I need. 我期望获得可以在控制台上打印的内容,而不是将其重定向到具有所需信息的JSON文件。

The act of submitting a form triggers browser navigation to the response returned from the HTTP request. 提交表单的行为触发浏览器导航到HTTP请求返回的响应。

The only way to stop it replacing the current page is to make the navigation happen elsewhere (eg in a frame or new window) with target … but then cross-origin security would prevent you from reading the data with JavaScript. 停止其替换当前页面的唯一方法是使导航发生在具有target …的其他位置(例如,在框架或新窗口中),但是跨域安全性将阻止您使用JavaScript读取数据。


You can't use a form to read data from a remote site for the same reasons you can't use Ajax to do it . 出于与无法使用Ajax进行操作的相同原因, 不能使用表单从远程站点读取数据。

The browser is designed to stop your JavaScript from reading data that someone else's website is willing to share with the owner of the browser . 浏览器旨在阻止您的JavaScript读取他人网站愿意与浏览器所有者共享的数据

If the data is public (ie doesn't need the user to log in, be on the same LAN, or anything similar) then use server-side technology to read it (you can proxy it to the browser). 如果数据是公开的(即不需要用户登录,不在同一LAN上或类似的东西),则使用服务器端技术读取数据(您可以将其代理到浏览器)。

Figured out you can add https://cors-anywhere.herokuapp.com/ to your endpoint and it will just work, even with Ajax requests. 弄清楚了您可以将https://cors-anywhere.herokuapp.com/添加到您的端点,它即使在Ajax请求中也可以正常工作。

Example

var apikey = "my-api-key";
var cors = "https://cors-anywhere.herokuapp.com/";
var endpoint = "https://test/example";
$.ajax({
        type: "POST",
        url: cors + endpoint + "/create",
        crossDomain: true,
        dataType: 'json',
        data: {
            'chave': apikey,
            'valor': 125.00
        },

        success: function (data) {
            console.log(data);
        },
        error: function (error) {
            console.log(error);
        },

    });

How or why this works? 这如何或为什么起作用? I have no clue. 我没有任何线索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM