Angularjs 角柱
我有以下代码,当我提交请求时;我收到的请求失败了。数据不会发送到服务器。知道为什么吗 所有我需要的是一个简单的表单提交与下面的代码。我认为问题在于我在哪里使用config部分收集表单数据。我环顾四周,我就是这样加上去的。任何帮助都将不胜感激 谢谢Angularjs 角柱,angularjs,angularjs-scope,Angularjs,Angularjs Scope,我有以下代码,当我提交请求时;我收到的请求失败了。数据不会发送到服务器。知道为什么吗 所有我需要的是一个简单的表单提交与下面的代码。我认为问题在于我在哪里使用config部分收集表单数据。我环顾四周,我就是这样加上去的。任何帮助都将不胜感激 谢谢 <!-- Main Content --> <div class="container"> <div class="page-header" ng-controller="NotesCtrl
<!-- Main Content -->
<div class="container">
<div class="page-header" ng-controller="NotesCtrl">
<h1>{{title}}</h1>
<form class="row-height" name="Form1" ng-submit="processForm('ajaxSubmitResult')">
<!--ng-submit="processForm(formData, 'ajaxSubmitResult') -->
First Name:
<input type="text" ng-model="formData.firstname" class="spacer" required/>
Last Name:
<input type="text" ng-model="formData.lastname" class="spacer" required/>
<!--<button class="btn btn-primary" ng-click="insert(formData)">Add</button>-->
<button type="submit" class="btn btn-success">Add Name</button>
</form>
<hr/>
<h4>Raw Data</h4>
{{formData}}
<hr/>
<h4>Submit Results</h4>
<span id="submitDebugText">{{ajaxSubmitResult | json}}</span>
</div>
</div>
<script>
// define angular module/app
var formApp = angular.module('formApp', []);
function NotesCtrl($scope, $http) {
$scope.title = "Test App";
$scope.formData = {};
$scope.processForm = function (resultVarName) {
var config = {
params: {
'firstname': $scope.firstname,
'lastname': $scope.lastname
}
};
$http.post("//", null, config)
.success(function (data, status, headers, config) {
$scope[resultVarName] = data;
})
.error(function (data, status, headers, config) {
$scope[resultVarName] = "bugger! Errors.";
});
};
}
</script>
{{title}}
名字:
姓氏:
添加名称
原始数据
{{formData}}
提交结果
{{ajaxSubmitResult}json}
//定义角度模块/应用程序
var formApp=angular.module('formApp',[]);
函数NotesCtrl($scope,$http){
$scope.title=“测试应用程序”;
$scope.formData={};
$scope.processForm=函数(resultVarName){
变量配置={
参数:{
“firstname”:$scope.firstname,
“lastname”:$scope.lastname
}
};
$http.post(“/”,null,config)
.success(函数(数据、状态、标题、配置){
$scope[resultVarName]=数据;
})
.error(函数(数据、状态、标题、配置){
$scope[resultVarName]=“bugger!错误。”;
});
};
}
在视图中执行类似操作时:
<input type="text" ng-model="formData.firstname" class="spacer" required/>
这个问题似乎是您没有在此处包含formData
:
var config = {
params: {
'firstname': $scope.firstname,
'lastname': $scope.lastname
}
};
换成
var config = {
params: {
'firstname': $scope.formData.firstname,
'lastname': $scope.formData.lastname
}
};
你应该准备好了。
$http.post(“/”
)-你为什么在那里有两个斜杠?我只是在测试。我只是想在数据发布之前查看数据。有没有办法调试和查看发生了什么事……你试过console.log
?
var config = {
params: {
'firstname': $scope.formData.firstname,
'lastname': $scope.formData.lastname
}
};