我正在学习Angularjs,并且我创建了简单的表单。其实我是PHP开发人员,所以我更愿意使用PHP作为服务器端脚本语言。我无法提交数据到服务器,我尝试了很多方法,但这些都非常复杂,如果我在标准方法尝试Angularjs不工作,请检查我的代码,并给我最好的方法来使用angularjs,jQuery和PHP。帮我!Angularjs和jquery不能以我的常规简单形式工作
angular.module("mainModule", [])
.controller("mainController", function($scope, $http) {
$scope.person = {};
$scope.serverResponse = '';
$scope.submitData = function() {
var config = {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
};
$http.post("server.php", $scope.person, config)
.success(function(data, status, headers, config) {
$scope.serverResponse = data;
})
.error(function(data, status, headers, config) {
$scope.serverResponse = "SUBMIT ERROR";
});
};
});
<?php
if (isset($_POST["person"]))
{
// AJAX form submission
$person = json_decode($_GET["person"]);
$result = json_encode(array(
"receivedName" => $person->name,
"receivedEmail" => $person->email));
} else
{
$result = "INVALID REQUEST DATA";
}
echo $result;
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body ng-app="mainModule">
<div ng-controller="mainController">
<form name="personForm1" novalidate ng-submit="submitData()">
<label for="name">First name:</label>
<input id="name" type="text" name="name" ng-model="person.name" required />
<br />
{{person.name}}
<br />
<label for="email">email:</label>
<input id="email" type="text" name="email" ng-model="person.email" data-parsley-type="email" required />
<br />
<br />
<button type="submit">Submit</button>
</form>
<br />
<div>
{{$scope.serverResponse}}
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!--<script type="text/javascript" src="script/parsley.js"></script>
<script src="script.js"></script>-->
</body>
</html>
您需要在您的html源代码中指定'ng-app' – 2015-02-08 11:00:47
您的HTML代码中存在拼写错误,请检查'{{person.name}}'。另外 - 更具体的什么不工作。你没有从服务器获得答案吗?数据没有发送? – simeg 2015-02-08 11:09:08
你面临什么问题? – 2015-02-08 11:09:14