在React JS中通过传递POST表单数据来重定向到外部URL?

huangapple go评论68阅读模式
英文:

Redirect to an external URL by passing post form data in React JS?

问题

我在页面中有一个表单,当我提交表单时,我应该调用一个API,该API的结果,让它是示例数据,如下,

> { "sample1" : "abc", "sample2" : "def" }

应该被重定向到一个外部URL,这个示例数据对象应该作为表单数据传递到URL。

我尝试使用AJAX,但似乎不起作用,

$.ajax({
    type: 'POST',
    url: EXTERNAL_URL,
    data: sampleData ,
    success: function(result) {
        console.log("success!")
    }
});

有人能帮我找到正确的方法吗?

英文:

I have a form in my page and when I submit the form I should call an API and that API's result, let it be sample data like,

> { "sample1" : "abc", "sample2" : "def" }

should be redirected to an external URL and this sample data object should be passed as form data to the URL.

I tried using AJAX but it doesn't seem to work,

 $.ajax({
                type: 'POST',
                url: EXTERNAL_URL,
                data: sampleData ,
                success: function(result) {
                    console.log("success!")
                }
            });

Can someone help me with the right approach for this?

答案1

得分: 1

以下是翻译好的内容:

关于以表单数据方式发送:
要以表单数据方式发送,您需要使用一种不太正规的方法来实现此目标,因为最初将表单数据发送到URL是用于提交表单的。

您可以参考这里的答案:创建具有不可见输入元素的表单

太长没看
创建一个带有不可见输入元素的表单,然后人为地提交此表单并转到外部链接:

function goToExternalURL() {
  const objectForSending = { "sample1" : "abc", "sample2" : "def" };

  const form = document.createElement('form');
  form.method = 'POST';
  form.action = 'http://externalurl.com';

  // 样本 1
  const hiddenFieldSample1 = document.createElement('input');
  hiddenFieldSample1.type = 'hidden';
  hiddenFieldSample1.name = 'sample1';
  hiddenFieldSample1.value = objectForSending.sample1;
  form.appendChild(hiddenFieldSample1);

  // 样本 2
  const hiddenFieldSample2 = document.createElement('input');
  hiddenFieldSample2.type = 'hidden';
  hiddenFieldSample2.name = 'sample2';
  hiddenFieldSample2.value = objectForSending.sample2;
  form.appendChild(hiddenFieldSample2);

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

关于发送查询参数:
或者,您可以使用查询参数方式发送,但这会将数据暴露在URL中:

使用两个独立的查询参数:
对于此解决方案,您只需导航到具有来自您的对象的名称的 2 个查询参数的URL:

const objectForRedirect = { "sample1" : "abc", "sample2" : "def" };

window.location.href = `redirecturl.com/?sample1=${objectForRedirect.sample1}&sample2=${objectForRedirect.sample2}`;

将序列化对象作为查询参数值发送:

const objectForRedirect = { "sample1" : "abc", "sample2" : "def" };

// 序列化对象并为URL做准备
const strinfigiedObject = JSON.stringify(objectForRedirect); // '{"sample1":"abc","sample2":"def"}'
const serializedObject = encodeURIComponent(strinfigiedObject); // '%7B%22sample1%22%3A%22abc%22%2C%22sample2%22%3A%22def%22%7D'

window.location.href = `redirecturl.com/?param=${serializedObject}`;

在接收端,您可以执行相反的操作:

const decodedParamValue = decodeURIComponent(valueFromQueryParam);

const parseStringToObject = JSON.parse(decodedParamValue);
英文:

For the form data way of sending, you would need to make a hacky way for achieving this goal, since sending form data to a URL was originally used for submitting a form.

You can refer to this answer here: Create invisible form with input elements

TL DR<br/>
Create a form with invisible input elements and submit artificially this form and go to your external link:

function goToExternalURL() {
  const objectForSending = { &quot;sample1&quot; : &quot;abc&quot;, &quot;sample2&quot; : &quot;def&quot; };

  const form = document.createElement(&#39;form&#39;);
  form.method = &#39;POST&#39;;
  form.action = &#39;http://externalurl.com&#39;;

  // Sample 1
  const hiddenFieldSample1 = document.createElement(&#39;input&#39;);
  hiddenFieldSample1.type = &#39;hidden&#39;;
  hiddenFieldSample1.name = &#39;sample1&#39;;
  hiddenFieldSample1.value = objectForSending.sample1;
  form.appendChild(hiddenFieldSample1);

  // Sample 2
  const hiddenFieldSample2 = document.createElement(&#39;input&#39;);
  hiddenFieldSample2.type = &#39;hidden&#39;;
  hiddenFieldSample2.name = &#39;sample2&#39;;
  hiddenFieldSample2.value = objectForSending.sample2;
  form.appendChild(hiddenFieldSample2);

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


Sending query params:

Alternatively you can use query params way of sending, but that exposes the data in the URL:

2 separate query parameters

For this solution you can just navigate to an URL with 2 query params with names from your object:

const objectForRedirect = { &quot;sample1&quot; : &quot;abc&quot;, &quot;sample2&quot; : &quot;def&quot; };

window.location.href = `redirecturl.com/?sample1=${objectForRedirect.sample1}&amp;sample2=${objectForRedirect.sample2}`;

Send serialized object as query param value

const objectForRedirect = { &quot;sample1&quot; : &quot;abc&quot;, &quot;sample2&quot; : &quot;def&quot; };

// Stringify the object and prepare it for URL
const strinfigiedObject = JSON.stringify(objectForRedirect); // &#39;{&quot;sample1&quot;:&quot;abc&quot;,&quot;sample2&quot;:&quot;def&quot;}&#39;
const serializedObject = encodeURIComponent(strinfigiedObject); // &#39;%7B%22sample1%22%3A%22abc%22%2C%22sample2%22%3A%22def%22%7D&#39;

window.location.href = `redirecturl.com/?param=${serializedObject}`;

On the receiving side you can just do the opposite operation:

const decodedParamValue = decodeURIComponent(valueFromQueryParam);

const parseStringToObject = JSON.parse(decodedParamValue);

huangapple
  • 本文由 发表于 2023年3月8日 17:48:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定