表格未通过angularjs提交

时间:2015-12-30 07:30:39

标签: php angularjs json symfony twig

我正在尝试使用symfony 2.8脚本

提交angularJS表单

我的symfony形式枝条如下:

<div ng-app="mediqApp" ng-controller="mediqController">
    {{ form_start(form, {'attr':{'id': 'mediForm', 'ng- submit':'processMediqForm()'}}) }}
    {{ form_row(form.name, {'attr':{'ng-model':'formData.name'}}) }}
    {{ form_row(form.description, {'attr':{'ng-model':'formData.description'}}) }}
    <div><input type="submit" value="Save"/></div>
    {{ form_end(form) }}
 </div>

我的角度js脚本如下:

var mediqApp = angular.module("mediqApp", []).controller("mediqController",function($scope, $http, $log){
$scope.formData = {};
$scope.processMediqForm = function() {
    $http({
        method  : 'POST',
        url     : "{{ path('medicine_new') }}",
        data    : $.param($scope.formData), 
        headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        })
        .success(function(data) {
            console.log(data.message);
        });
    };
});

当我按代码formData检查我的console.log($scope.formData)值时,我得到的输入值就像这个对象{name: "Crocin", description: "Help in Headache, little fever"}

但是当我尝试提交表单时,我收到的错误是这样的:

SyntaxError: Unexpected token <
at Object.parse (native)
at pc (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:14:219)
at Yb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:76:201)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:77:22
at r (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:7:302)
at Wc (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:77:4)
at c (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:78:109)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:110:505
at k.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:124:325)
at k.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:121:427)

我无法理解我做错了什么。

1 个答案:

答案 0 :(得分:2)

最后我通过以下步骤解决了我的问题..

在symfony 2.8中,为了创建一个新的类的瞬间,newAction和createAction方法都被合并,并且只应用了一个方法newMethod。

我再次将它们分解为newAction和createAction,就像之前的symfony版本一样。

在My form twig我喜欢这样:

<div ng-app="mediqApp" ng-controller="mediqController">
  <form method="POST" action="#" name="mediForm">
    {{ form_start(form) }}
    {{ form_row(form.name) }}
    {{ form_row(form.description) }}
    <div><input type="submit" value="Save" ng-click="processMediqForm()"/></div>
    {{ form_end(form) }}
  </form>
</div>

在angularjs脚本中,我喜欢这样:

var mediqApp = angular.module("mediqApp", []).controller("mediqController",function($scope, $http, $log){
        $scope.processMediqForm = function() {
            $http({
                method: 'POST',
                url: "{{ path('medicine_create') }}",
                data: $('form[name=mediForm]').serialize(),
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
              }).success(function(response) {
                $scope.result = response.data;
                console.log($scope.result);
              });
            }
        });

我基本上将输入数据作为表格序列化数据传递。现在它工作得很好..

再次感谢所有人的建议......