Dynamic action in HTML form with POST method

My webapp should redirect the user to another website on which I do not control. This site expects to receive several POST parameters from the user.

Without angular, I would have to redirect the user using the following form:

<FORM METHOD="POST" ACTION="https://otherwebsite.com/index.php"> 
  <INPUT TYPE="HIDDEN" NAME="id" VALUE="A1S2D3"> 
  <INPUT TYPE="hidden" NAME="number" VALUE="1234567"> 
  <INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="Submit"> 
</FORM>

In angular, this will also work, but I want the action url and the number value to come from the angular scope.

Unfortunately, this does not work when I use ng-model or {{var}} in the value / action attributes.

Any idea to redirect the user to another site with POSTed options from the area?

+4
source share
3 answers

Here I found an easy way to use it, which (kinda uses) Angular.

I have an angular function

$scope.setAction= function(){
    document.myForm.action='http://anothersite.com';
};

myForm - .

<form action="http://asamplesite.com" method="POST" name="myForm">

. http://jsfiddle.net/nicolasmoise/f6R9x/

"Angular Way", , , , . , , , angular . ng-submit, url / (, $location). , , , .

+4

ngSubmit, . , , - x-action, . .

  <script>
    function Ctrl($scope) {
      $scope.number = "1234567";
      $scope.id = "A1S2D3";
    }
  </script>

  <form ng-submit ng-controller="Ctrl" data-action="https://otherwebsite.com/index.php">
    <INPUT TYPE="HIDDEN" NAME="id" ng-model="id">
    <INPUT TYPE="hidden" NAME="number" ng-model="number">
    <input type="submit" id="submit" value="Submit" />
  </form>
0

I had the same problem with hidden inputs (it seems that angular ignores them).

I used this solution:

<INPUT TYPE="text" NAME="number" ng-model="myNumber" hidden> 
0
source

Source: https://habr.com/ru/post/1531028/


All Articles