2013-07-31 45 views
5

我正在使用服務來更新數據庫表。

myApp.factory('createGal', function ($http, $q) 
{ 
    return { 
     createGal: function() 
     { 
      var deferred = $q.defer(); 
      var newGalleryArray = {}; 

      newGalleryArray.galleryName = 'New Image Gallery'; 
      newGalleryArray.client  = 245; 

      $http.post('/beta/images/create', {newGalleryArray: newGalleryArray}).success(function(data) 
      { 
       console.log(data); 
       deferred.resolve(data); 
      }); 

      return deferred.promise; 
     } 
    }; 
}); 

PHP

public function create() 
{ 
    print_r($_POST); 
} 

陣列被返回空。我錯誤地傳遞數組?

Chrome瀏覽器開發 enter image description here

感謝

+0

控制檯對請求的說明是什麼? – tymeJV

+0

成功,請求。返回數據庫操作,只是不在POST中發送的數組 – Bungdaddy

+0

有趣...您的PHP發送回來了什麼? – tymeJV

回答

6

這已經有一段時間,因爲我用PHP,但不$_POST只包含請求PARAMATERS? $http.post通過JSON有效負載發送數據,而不是請求參數。因此,您需要使用類似於json_decode

+0

DOH!只需要添加$ json = json_decode(file_get_contents(「php:// input」)); – Bungdaddy

+0

如果你有jQuery的一方通過$ .params發送你的發佈數據,並添加內容類型'application/x-www-form-urlencoded'到請求頭。這樣你可以使用$ _POST全局。 – Oliver