[英]Angular POST to API doesn't pass data
所以我在我的項目中使用makoframework和AngularJS,目前遇到了一些問題。
因此,在我的表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"
這意味着當我使用Chrome的REST客戶端時,數據不會傳遞給API。
我在這里錯過了什么嗎? 任何幫助將非常感激。
更新:
首先,我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對象?
默認情況下$ http POST在json中發送數據,所以你需要在PHP中解析json字符串才能訪問name屬性,或者你可以將$ http params中的頭文件更改為application / x-www-form-urlencoded如果你想在提交表格時獲取參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.