我有一个angularJS表单将数据发布到Java Servlet,但我没有看到请求通过; servlet“create”没有被调用。
这是我的代码:
的test.html
<body> <form ng-controller="UserController"> <legend>Create User</legend> <label>Name</label> <input type="text" id="name" name="name" ng-model="name" placeholder="User Name"> <label>Email</label> <input type="text" id="email" name="email" ng-model="email" placeholder="ur email here"> <label>Password</label> <input type="text" id="pwd" name="pwd" ng-model="pwd" placeholder="ur own pwd here"> <button ng-submit="createUser()" class="btn btn-primary">Register</button> </form> </body>
的script.js
function UserController($scope,$http) { $scope.user = {}; $scope.createUser = function() { $http({ method : 'POST',url : '/create',data : 'name=' + $scope.user.name + '&email=' + $scope.user.email,headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } }) }
我的servlet如下,但不打印“发布”全部。
public class FirstServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException { System.out.println("Get"); } @Override protected void doPost(HttpServletRequest req,IOException { System.out.println("Post"); } }
web服务器是jetty,web.xml如下所示:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>createUser</servlet-name> <servlet-class>servlet.FirstServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>createUser</servlet-name> <url-pattern>/create</url-pattern> </servlet-mapping> </web-app>
要将数据发布到Web服务器,您将需要将表单值绑定到$ scope中的对象,然后将该对象提交给脚本。
原文链接:https://www.f2er.com/angularjs/144324.html诀窍是将整个对象“user”提交到服务器,Angular将自动将其格式化为JSON。另外,ng模型标签没有使用“user”。
另外需要注意的是,在完成请求时,您可能希望为应用程序添加一些内容。您可以使用“.success(function(data){})”和“.error(…)”这些方法(这些是promise $ http返回的方法)。
我已经包括PHP和Servlet代码;但是,对于所有服务器脚本(来自Angular的JSON数据)也是如此。
HTML
<body> <form ng-controller="UserController" ng-submit="createUser()"> <legend>Create User</legend> <label>Name</label> <input type="text" id="name" name="name" ng-model="user.name" placeholder="User Name"> <label>Email</label> <input type="text" id="email" name="email" ng-model="user.email" placeholder="ur email here"> <label>Password</label> <input type="text" id="pwd" name="pwd" ng-model="user.pwd" placeholder="ur own pwd here"> <button class="btn btn-primary">Register</button> </form> </body> </html>
调节器
function UserController($scope,data : $scope.user }) }
$data = file_get_contents("PHP://input"); $objData = json_decode($data); $pwd = $objData -> pwd; $user = $objData -> name; //etc
示例服务器代码:JAVA Servlet
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"); }