我正在学习 Angularjs 并且我创建了简单的表单。实际上我是 PHP 开发人员,所以我更喜欢使用 php 作为服务器端脚本语言。我无法将数据提交到服务器,我尝试了很多方法,但如果我尝试使用标准方法,这些方法非常复杂 Angularjs 无法正常工作,请检查我的代码,并提供使用 angularjs、jquery 和 php 的最佳方法。帮帮我!
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>
最佳答案
从你的代码看来,你误解了一些概念。
- 你根本没有使用 jQuery - 你可以删除它,除非解析(不熟悉这个......)需要它
- 除
DOCTYPE
之外的所有HTML 标签应该在根里面html
标签。建议在body
底部添加 JS将(概念上)有助于页面加载性能的标记。 - 您导入 JS 的顺序很重要,应该按依赖项排序(例如:AngularJS 只有在包含 jQuery 时才能使用它,但在您的情况下,angular 不知道它,因为 jQuery 是在 AngularJS 之后添加的,这会导致 Angular 构建取而代之的是 jq lite)
- 您添加了一个
submitData
到你的 Controller 的范围,但你从来没有调用它 - 你的意图可能是在用户提交表单时使用它,所以你需要删除action
和method
属性并添加ng-submit
:<form name="personForm1" method="post" novalidate ng-submit="submitData(person, 'thePropNameOnWhichYouWantToSaveTheReturnedData')">
.这两个参数都是多余的,因为你在$scope
上有它们. config
与$http
一起发送的参数服务用于配置,而不是数据。在这里阅读:Angular $http$http
的默认行为正在发送 JSON 作为请求的主体。看来您希望在您的 PHP 代码中有一个表单。这可以在config
中更改例如,或者您可以学习如何在 PHP 上反序列化 JSON(抱歉,我不懂 PHP)。- 将要保存数据的属性添加到
$scope
所以它可以被渲染。
固定客户端代码建议:
angular.module("mainModule", [])
.controller("mainController", function($scope, $http) {
$scope.person = {};
$scope.serverResponse = '';
$scope.submitData = function() {
// Let's say that your server doesn't expect JSONs but expects an old fashion submit - you can use the `config` to alter the request
var config = {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
};
$http.post("server.php", $scope.person, config) // You can remove the `config` if the server expect a JSON object
.success(function(data, status, headers, config) {
$scope.serverResponse = data;
})
.error(function(data, status, headers, config) {
$scope.serverResponse = "SUBMIT ERROR";
});
};
});
<!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>
您还应该在 AngularJS docs 上阅读更多内容也许做他们的完整教程。非常有用
关于javascript - Angularjs 和 jquery 不能以我的常规简单形式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28393141/