Php 表格未通过angularjs提交
我正在尝试使用Php 表格未通过angularjs提交,php,angularjs,json,symfony,twig,Php,Angularjs,Json,Symfony,Twig,我正在尝试使用angularJS脚本提交symfony 2.8表单 我的symfony表单小枝如下: <div ng-app="mediqApp" ng-controller="mediqController"> {{ form_start(form, {'attr':{'id': 'mediForm', 'ng- submit':'processMediqForm()'}}) }} {{ form_row(form.name, {'attr':{'ng-model'
angularJS
脚本提交symfony 2.8
表单
我的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>
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);
});
};
});
当我通过codeconsole.log($scope.formData)
检查我的formData
值时,我得到的输入值类似于这个对象{name:“Crocin”,description:“Help in header,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)
SyntaxError:意外标记<
at Object.parse(本机)
在pc机上(https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:14:219)
在Yb(https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:76:201)
在https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:77:22
在r(https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:7:302)
在Wc(https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:77:4)
在c(https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:78:109)
在https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:110:505
以k$eval的价格(https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:124:325)
售价k$digest(https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js:121:427)
我无法理解我做错了什么。最后我通过以下步骤解决了我的问题 在symfony 2.8中,为了创建类的新瞬间,合并了newAction和createAction方法,并且只应用了一个方法newMethod 我再次将它们分为newAction和createAction,就像以前的symfony版本一样 在我的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>
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);
});
}
});
我基本上是以表单序列化数据的形式传递输入数据。现在它工作得很好
再次感谢大家的建议……您的url是错误的:
url:{{path('medicine_new')}},
,它应该是一个字符串,类似于:url:/medicine/new“,
这是可以的,因为在symfony2中url是通过这个方法传递的,最后我解决了我的问题,在formData中我传递了错误的凭证。已经解决了。非常感谢你的帮助!!因此,您缺少AUTH
标题或类似的内容?@Ajeet您应该更新您的问题,解释您是如何解决问题的!嗨,阿维吉特,请看我的答案。。