2016-11-04 43 views
0

我剛開始嘗試學習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> 

當應用程序加載時,初始狀態爲「清單」和所有的數據庫中的項目在頁面上可見。

列表中的每個項目都有一個編輯按鈕。當我點擊一個編輯按鈕時,狀態變爲'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=$1 [QSA,NC,L] 

RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^(.*)$ api.php [QSA,NC,L] 

RewriteCond %{REQUEST_FILENAME} -s 
RewriteRule ^(.*)$ api.php [QSA,NC,L] 
</IfModule> 
+0

達米安嗨,你從得到什麼:的var_dump( $ _ POST); – Wils

+0

我試過error_log(var_dump($ _ POST));但所有我在eror日誌中得到了stderr:,referer:http:// localhost:4400/index.html – Damian

+0

嗨,我想var_dump的結果不是:「error_log(var_dump($ _ POST));」。 請只有「var_dump($ _ POST);」 和localhost將無法訪問我,您可以發送給我的IP地址。如果你想要我可以快速瀏覽一下,請加我skype:wils_br – Wils

回答

1

感謝您的回覆傢伙。

@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']);