所以我使用makoframework和AngularJS爲我的項目,目前有一些問題。對API的角度POST不傳遞數據
所以在我的表customers
,customer_name
字段是不可空的。我現在的代碼從哪裏得到customers
表中的所有數據就是這樣。
function CustomerCtrl($scope, Customers, DTOptionsBuilder) {
$scope.loadCustomers = function() {
Customers.getCustomers()
.then(function (response) {
$scope.customers = response.data;
}, function (error) {
console.log(error);
});
}
}
//factory
function Customers($http) {
return {
getCustomers: function() {
return $http.get('/api/customers');
},
addCustomer: function (data) {
return $http({
method: 'POST',
url: '/api/customers',
data: data
});
}
}
的/api/customers
返回一個JSON響應,並與我的角碼效果很好。
現在我在我的php中有另一條路線,它是/api/products POST
,當我嘗試使用Chrome的Advanced Rest Client
添加數據時,它也起作用。
但是,當使用我的角碼,它不工作已經。
我的代碼
$scope.addCustomer = function() {
alert($scope.customerData.full_name); // THIS ALERT IS WORKING
var data = {
'name': $scope.customerData.full_name,
}
Customers.addCustomer(data)
.then(function (response) {
$scope.customerSpinner = true;
var message = response.data.message;
console.log(response);
$scope.customerData = {};
$scope.loadCustomers();
Materialize.toast('<i class="mdi-navigation-check left"></i> ' + message, 3000, 'green darken-2 white-text');
}, function (error) {
console.log(error);
});
}
// HTML
<div id="addCustomerModal" class="modal">
<div class="modal-content">
<h4>Add Customer</h4>
<div class="row">
<form action="" class="col s12">
<div class="input-field col s6">
<input id="full_name" type="text" class="validate" ng-model="customerData.full_name">
<label for="full_name">Full Name</label>
</div>
</form>
</div>
</div>
<div class="modal-footer">
<a class="waves-effect btn-flat teal lighten-2 white-text" ng-click="addCustomer()">Submit <i class="mdi-content-send right"></i></a>
</div>
</div>
// /api/products
$routes->post('/api/customers', 'app\controllers\CustomerController::create');
public function create()
{
// take note that this works using a restful client from chrome
$customer = new Customer();
$customer->customer_name = $this->request->post('name');
if($customer->save()) {
$result = array('success' => true, 'message' => 'Customer '.$this->request->post('name').' has been added.');
}
else {
$result = array('success' => false, 'message' => 'Error adding customer.');
}
return $this->jsonResponse($result);
}
正如您在addCustomer $scope
中看到的那樣,我添加了一個alert($scope.customerData.full_name);
,它實際上可行。我得到的數據。
但我得到一個SQL錯誤說
"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'customer_name' cannot be null"
,這意味着數據不傳遞給API雖然當我使用REST客戶端,從Chrome工作。
我在這裏錯過了什麼嗎?任何幫助將非常感激。
UPDATE:
首先,我JSON字符串化的數據變量:
var data = JSON.stringify({
'name': $scope.customerData.full_name,
});
和更改的內容類型application/x-www-form-urlencoded
addCustomer: function (data) {
return $http({
method: 'POST',
url: '/api/customers',
data: data,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
}
而且在我的PHP控制器我想:
return $this->jsonResponse($this->request->post());
而且我得到這個:
現在我只需要訪問它。當我嘗試$this->request->post('name')
時,仍然出現錯誤。那麼我怎樣才能正確地獲得json對象?
如果你在'Customers.addCustomer(data)'之前添加'console.log(data)',你需要顯示你的''/ api/products POST'的代碼' – CodeGodie
'你會得到什麼? – CodeGodie
@CodeGodie我在我們的'create()'方法的第一行中得到這個http://i.imgur.com/BnTJ2EW.png(不介意其他未定義的) – FewFlyBy