Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To post data to a web server, you will want to bind your form values to a object in the $scope, and then submit that object to the script.</p> <p>The trick is to submit the entire object "user" to the server, and Angular will automatically format it in JSON. Also, "user" was not being used by the ng-model tags.</p> <p>Another thing to note is that you will probably want to include something for the app to do when it finishes the request. You can use the methods ".success(function(data){})" and ".error(...)" to do this (these are methods on the promise $http returns).</p> <p><strong>I've included both PHP and Servlet code; it is the same, however, for all server scripts (JSON data from Angular).</strong></p> <p><strong>HTML</strong></p> <pre><code>&lt;body&gt; &lt;form ng-controller="UserController" ng-submit="createUser()"&gt; &lt;legend&gt;Create User&lt;/legend&gt; &lt;label&gt;Name&lt;/label&gt; &lt;input type="text" id="name" name="name" ng-model="user.name" placeholder="User Name"&gt; &lt;label&gt;Email&lt;/label&gt; &lt;input type="text" id="email" name="email" ng-model="user.email" placeholder="ur email here"&gt; &lt;label&gt;Password&lt;/label&gt; &lt;input type="text" id="pwd" name="pwd" ng-model="user.pwd" placeholder="ur own pwd here"&gt; &lt;button class="btn btn-primary"&gt;Register&lt;/button&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Controller</strong></p> <pre><code>function UserController($scope, $http) { $scope.user = {}; $scope.createUser = function() { $http({ method : 'POST', url : '/create', data : $scope.user }) } </code></pre> <p><strong>Example Server Code: PHP</strong></p> <pre><code>$data = file_get_contents("php://input"); $objData = json_decode($data); $pwd = $objData -&gt; pwd; $user = $objData -&gt; name; //etc </code></pre> <p><strong>Example Server Code: JAVA Servlet</strong></p> <pre><code>JSONObject jObj = new JSONObject(request.getParameter("mydata")); // this parses the json Iterator it = jObj.keys(); //gets all the keys while(it.hasNext()) { String key = it.next(); // get key Object o = jObj.get(key); // get value //do something with it here //you can also do: String user = jObj.get("user"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload