javascript - How to open a new tab with a post request from a component in Angular - Stack Overflow

I am trying to build a function which sends a request post to a url, and at the same time open that url

I am trying to build a function which sends a request post to a url, and at the same time open that url in a new tab with the post request...

This is what i got so far...

OLD

pay() {
    var data = `{
      "description": "Test PAYU",
      "referenceCode": "TestPayU",
      "amount": "20000",
      "tax": "3193",
      "buyerEmail": "[email protected]",
      "responseUrl": ";,
      "confirmationUrl": ";,
      "submit": ""
    }`;
    this.http.post('/', data).subscribe( (response)=>{
      console.log(response);
     });
  }

I want to open that url in a new tab within the request post on it..

I don't know how to do that with Angular...

UPDATE

So, I have done this solution for this problem, adding this dynamic form with javascript to do that:

pay() {
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", ";);
    form.setAttribute("target", "view");
    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("merchantId", "508029");
    hiddenField.setAttribute("accountId", "512321");
    hiddenField.setAttribute("description", "Test PAYU");
    hiddenField.setAttribute("referenceCode", "TestPayU");
    hiddenField.setAttribute("amount", "20000");
    hiddenField.setAttribute("tax", "3193");
    hiddenField.setAttribute("taxReturnBase", "16806");
    hiddenField.setAttribute("currency", "COP");
    hiddenField.setAttribute("signature", "7ee7cf808ce6a39b17481c54f2c57acc");
    hiddenField.setAttribute("test", "1");
    hiddenField.setAttribute("buyerEmail", "[email protected]");
    hiddenField.setAttribute("responseUrl", ";);
    hiddenField.setAttribute("confirmationUrl", ";);
    hiddenField.setAttribute("submit", "");
    form.appendChild(hiddenField);
    document.body.appendChild(form);
    window.open('', 'view');
    form.submit();
}

The problem is, even if the page is opened in a new tab, the parameters are not sent to the page...

What could be the problem?

I am trying to build a function which sends a request post to a url, and at the same time open that url in a new tab with the post request...

This is what i got so far...

OLD

pay() {
    var data = `{
      "description": "Test PAYU",
      "referenceCode": "TestPayU",
      "amount": "20000",
      "tax": "3193",
      "buyerEmail": "[email protected]",
      "responseUrl": "http://www.test./response",
      "confirmationUrl": "http://www.test./confirmation",
      "submit": ""
    }`;
    this.http.post('https://sandbox.checkout.payulatam./ppp-web-gateway-payu/', data).subscribe( (response)=>{
      console.log(response);
     });
  }

I want to open that url in a new tab within the request post on it..

I don't know how to do that with Angular...

UPDATE

So, I have done this solution for this problem, adding this dynamic form with javascript to do that:

pay() {
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", "https://sandbox.checkout.payulatam./ppp-web-gateway-payu");
    form.setAttribute("target", "view");
    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("merchantId", "508029");
    hiddenField.setAttribute("accountId", "512321");
    hiddenField.setAttribute("description", "Test PAYU");
    hiddenField.setAttribute("referenceCode", "TestPayU");
    hiddenField.setAttribute("amount", "20000");
    hiddenField.setAttribute("tax", "3193");
    hiddenField.setAttribute("taxReturnBase", "16806");
    hiddenField.setAttribute("currency", "COP");
    hiddenField.setAttribute("signature", "7ee7cf808ce6a39b17481c54f2c57acc");
    hiddenField.setAttribute("test", "1");
    hiddenField.setAttribute("buyerEmail", "[email protected]");
    hiddenField.setAttribute("responseUrl", "http://www.test./response");
    hiddenField.setAttribute("confirmationUrl", "http://www.test./confirmation");
    hiddenField.setAttribute("submit", "");
    form.appendChild(hiddenField);
    document.body.appendChild(form);
    window.open('', 'view');
    form.submit();
}

The problem is, even if the page is opened in a new tab, the parameters are not sent to the page...

What could be the problem?

Share Improve this question edited Apr 23, 2021 at 20:36 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Oct 26, 2020 at 18:55 Luis BermúdezLuis Bermúdez 65413 silver badges32 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

I solved my problem using this dynamic form with JavaScript:

pay() {
    var form = document.createElement("form");
    form.target = "view";
    form.method = "POST";
    form.action = "https://sandbox.checkout.payulatam./ppp-web-gateway-payu";
    var params = {
      "merchantId": "508029",
      "accountId": "512321",
      "description": "Test PAYU"
    };

    for (var i in params) {
        if (params.hasOwnProperty(i)) {
          var input = document.createElement('input');
          input.type = 'hidden';
          input.name = i;
          input.value = params[i];
          form.appendChild(input);
        }
    }

    document.body.appendChild(form);
    form.submit();
    window.open('', 'view');
    }

This did what i wanted, problem solved!.

pay() {
    var url = 'https://sandbox.checkout.payulatam./ppp-web-gateway-payu';
    var data = `{
      "description": "Test PAYU",
      "referenceCode": "TestPayU",
      "amount": "20000",
      "tax": "3193",
      "buyerEmail": "[email protected]",
      "responseUrl": "http://www.test./response",
      "confirmationUrl": "http://www.test./confirmation",
      "submit": ""
    }`;
    this.http.post(url, data).subscribe( (response)=>{
      window.open(`${url}?data=${encodeURI(data)}`, '_blank')
     });
  }

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信