AngularJS $resource 如何发送更新的数据?
How does AngularJS $resource send updated data?
我刚刚开始尝试学习 AngularJS 并且正在尝试让一个简单的应用程序正常工作,它只显示和更新 MySQL 数据库中的项目。
我有一个 PHP rest API 正在处理来自应用程序的请求。
到目前为止,API 已成功将数据库中的所有项目以 JSON 编码的数组传回。它还传回了一条记录,但我无法让它更新记录。
当我点击表单上的保存按钮时,我看不到更改的数据是如何从应用程序传递到 API 的。它应该作为 $_POST 变量传递吗?
这是我应用中的 JS 代码:
angular.module('starter.services', []).factory('List', function ($resource) {
return $resource('http://example.com/restAPI/:id', { id: '@id' }, {
update: {
method: 'PUT'
}
});
});
angular.module('starter', ['ionic', 'ngResource', 'starter.controllers', 'starter.services'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('list', {
url: '/',
templateUrl: 'templates/list.html',
controller: 'ListController'
})
.state('editItem', {
url: '/items/:id/edit',
templateUrl: 'templates/item-edit.html',
controller: 'EditController'
})
$urlRouterProvider.otherwise('/');
});
angular.module('starter.controllers', [])
.controller('ListController', function ($scope, $state, $stateParams, List) {
$scope.items = List.query();
})
.controller('EditController', function ($scope, $state, $stateParams, List) {
$scope.updateItem=function(){
$scope.item.$update(function(){
$state.go('list');
});
};
$scope.loadItem=function(){
$scope.item=List.get({id:$stateParams.id});
};
$scope.loadItem();
});
表单代码如下:
<form class="form-horizontal" role="form" ng-submit="updateItem()">
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input type="text" ng-model="item.title" class="form-control" id="title" placeholder="Title Here"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-primary" value="Save"/>
</div>
</div>
</form>
当应用程序加载时,初始状态为 'list',并且数据库中的所有项目都在页面上可见。
列表中的每一项都有一个编辑按钮。当我单击编辑按钮时,状态更改为 'editItem' 并且新页面加载了单个项目。到目前为止,还不错。
当我更改数据并点击保存按钮时,函数 updateItem 被调用 ng-submit="updateItem()"
。
这调用了 API,但我看不到它如何传递更新的数据。当我在 API 中尝试 error_log($_POST["title"]);
时,我在错误日志中看到 'PHP Notice: Undefined index: title'。如何访问 API 中更改的数据以便更新数据库?
这是我正在使用的 .htaccess - 这会不会有问题?这是我在网上找到的,所以我对它的工作原理知之甚少。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ api.php?id= [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ api.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ api.php [QSA,NC,L]
</IfModule>
感谢大家的回复。
@georgeawg - 你让我走上了正确的道路。我以前只使用过 GET 和 POST 而更新方法使用 PUT 正如你指出的那样。
意识到这一点后,我在这里找到了答案...
REST request data can't be read in 'put' method
这段代码对我有用....
$data = json_decode(file_get_contents("php://input"), true);
error_log($data['title']);
我刚刚开始尝试学习 AngularJS 并且正在尝试让一个简单的应用程序正常工作,它只显示和更新 MySQL 数据库中的项目。
我有一个 PHP rest API 正在处理来自应用程序的请求。
到目前为止,API 已成功将数据库中的所有项目以 JSON 编码的数组传回。它还传回了一条记录,但我无法让它更新记录。
当我点击表单上的保存按钮时,我看不到更改的数据是如何从应用程序传递到 API 的。它应该作为 $_POST 变量传递吗?
这是我应用中的 JS 代码:
angular.module('starter.services', []).factory('List', function ($resource) {
return $resource('http://example.com/restAPI/:id', { id: '@id' }, {
update: {
method: 'PUT'
}
});
});
angular.module('starter', ['ionic', 'ngResource', 'starter.controllers', 'starter.services'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('list', {
url: '/',
templateUrl: 'templates/list.html',
controller: 'ListController'
})
.state('editItem', {
url: '/items/:id/edit',
templateUrl: 'templates/item-edit.html',
controller: 'EditController'
})
$urlRouterProvider.otherwise('/');
});
angular.module('starter.controllers', [])
.controller('ListController', function ($scope, $state, $stateParams, List) {
$scope.items = List.query();
})
.controller('EditController', function ($scope, $state, $stateParams, List) {
$scope.updateItem=function(){
$scope.item.$update(function(){
$state.go('list');
});
};
$scope.loadItem=function(){
$scope.item=List.get({id:$stateParams.id});
};
$scope.loadItem();
});
表单代码如下:
<form class="form-horizontal" role="form" ng-submit="updateItem()">
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input type="text" ng-model="item.title" class="form-control" id="title" placeholder="Title Here"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-primary" value="Save"/>
</div>
</div>
</form>
当应用程序加载时,初始状态为 'list',并且数据库中的所有项目都在页面上可见。
列表中的每一项都有一个编辑按钮。当我单击编辑按钮时,状态更改为 'editItem' 并且新页面加载了单个项目。到目前为止,还不错。
当我更改数据并点击保存按钮时,函数 updateItem 被调用 ng-submit="updateItem()"
。
这调用了 API,但我看不到它如何传递更新的数据。当我在 API 中尝试 error_log($_POST["title"]);
时,我在错误日志中看到 'PHP Notice: Undefined index: title'。如何访问 API 中更改的数据以便更新数据库?
这是我正在使用的 .htaccess - 这会不会有问题?这是我在网上找到的,所以我对它的工作原理知之甚少。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ api.php?id= [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ api.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ api.php [QSA,NC,L]
</IfModule>
感谢大家的回复。
@georgeawg - 你让我走上了正确的道路。我以前只使用过 GET 和 POST 而更新方法使用 PUT 正如你指出的那样。
意识到这一点后,我在这里找到了答案... REST request data can't be read in 'put' method
这段代码对我有用....
$data = json_decode(file_get_contents("php://input"), true);
error_log($data['title']);