我正在學習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