而从发布JS角表单数据错误 [英] Error while posting data from angular js form

查看:177
本文介绍了而从发布JS角表单数据错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个angularJS形式职位数据到了Scalatra的servlet。当表单被提交我不能让我的了Scalatra servlet的任何形式的PARAMS。

I have an angularJS form which posts data to a scalatra servlet. When the form gets submitted I can't get any form params in my scalatra servlet.

下面是我的code

AngularJS

AngularJS

$scope.createUser = function() {
    $http.post('/createUser',{name:$scope.name,email:$scope.email,pwd:$scope.pwd}).
        success(function(data, status, headers, config) {
            alert("success " + data)
        }).
        error(function(data, status, headers, config) {
            alert("failure =>" +data)
        });
 };         });
 };     

HTML表单

<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-click="createUser()" class="btn btn-primary">Register</button>
        </form>

了Scalatra的Servlet

Scalatra Servlet

post("/createUser") {
    println(params("name")) 
}

当我运行应用程序并尝试从形式我得到这个错误提交

When I run the app and try to submit from the form I get this error

错误500键未发现:(从萤火虫精简版获得)名称

Error 500 key not found: name (obtained from firebug lite)

请让我知道如果荫缺少的东西,或电气特性的方式做到这一点。

Please let me know if Iam missing something or anyother way to do this

推荐答案

两个变化:


  1. 使用'NG提交事件。

  2. 把NG的模型对象本身里面,所以你可以只发送的对象在后。

HTML

<div ng-controller="UserController">
  <form ng-submit="createUser()">
    <input type="text" ng-model="user.name">
    ...
    <input type="email" ng-model="user.email">
    ...
  </form>
</div>

JS:

function UserController($scope, $http) {
  $scope.user = {};
  $scope.createUser = function() {
    $http.post('/createUser', $scope.user);
  }
}

这篇关于而从发布JS角表单数据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆