我想將我的HTTP POST請求包裝到一個服務中,然後這個服務可以用來從我的PHP API檢索數據,但問題是我不知道如何讓服務使用用戶輸入的數據。 這裏看看我的代碼Angular Post Service
<form ng-submit="myCtrl.submit()" name="myForm">
<input type="text" ng-model="myCtrl.user.name">
<input type="text" ng-model="myCtrl.user.message">
<input type="submit" value="Submit" ng-disabled="myForm.$invalid">
</form>
我的JS代碼
var app = angular.module('MyApp', []);
app.service('PostService', function($http) {
return{
success: function() {
return $http.post('process3.php', self.user);
}
};
});
app.controller('MyCtrl', ['PostService', function(PostService) {
var self = this;
var user = {};
self.submit = function() {
PostService.success().then(function(data) {
console.log(data.data);
});
}
}]);
PHP代碼:
<?php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$name = $request->name;
@$message = $request->message;
$records = array(
'name' => $name,
'message' => $message
);
echo json_encode($records);
?>
我不能找到解決這個問題,因爲我有搜索谷歌和Stackoverflow一次又一次。但我甚至不知道我所問的是不合邏輯的。但是,如果你能夠理解問題並有更好的方法來處理它,那麼請讓我知道。
基本上我想把我的POST請求包裝在一個服務中,就像我們使用GET服務一樣。
用戶在'success:function()'中作爲參數怎麼樣?喜歡'成功:功能(用戶)' – tpsilva
^他說什麼:) – Rob
是的,我只是想念的!感謝您回覆評論! :D – Drazxier