2015-10-17 95 views
1

從UI我撥打電話:爲什麼php服務不能獲取變量?

$http.post('services/loadCategory.php', { 
    'id' :'1', 
    'type' :'string' 
}).then(function(response) { 
    debugger; 
    ... 
}, function(response) {    
    ... 
}); 

在PHP服務,我無法從身體POST請求的變量:

include ("bd.php"); 
header("Content-type: text/html; charset=windows-1251"); 
// ----- ----- ----- ----- ----- 
if (isset($_POST['type'])) { 
    $type = $_POST['type']; 
} 
if (isset($_POST['id'])) { 
    $id = $_POST['id']; 
} 
// 
exit(json_encode(
    array('type' => iconv('windows-1251', 'UTF-8', $_POST['type']), 
      'id' => iconv('windows-1251', 'UTF-8', $_POST['id']) 
))); 

從服務請求:{ID: '',類型: '' }如何解決這個問題?

+0

'$ _POST'是否有任何內容? (使用'print_r()'來檢查)。 '$ _REQUEST'呢? – Spudley

回答

2

將JSON發佈到PHP時,$ _POST變量爲空。要獲取原始的JSON在PHP中,使用以下命令:

if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
    $data = json_decode(file_get_contents('php://input'), true); 
} 

然後,您可以用$data['id']訪問數據和$data['type']

print_r($data);

+0

Working))。我不明白 - 爲什麼簡單的$ _POST不起作用? –

+0

@DarienFawkes這是因爲大多數前端設置,如jQuery,使用Content-Type:x-www-form-urlencoded和common?key1 = foo&key2 = bar序列化傳輸POST數據。 AngularJS使用Content-Type:application/json和{「key1」:「foo」,「key2」:「bar」}。雖然這同樣好,但PHP並不知道如何處理這個本地問題。 – buzzsaw

0

檢查傳入$的數據做一個快速搜索後關於這個問題,似乎PHP很難對由AngularJS發送的POST主體進行反序列化。與大多數發送內容爲application/x-www-form-urlencoded的其他JavaScript變體相比,AngularJS發送的所有JSON編碼信息(application/json)。

要解決這個問題,您應該將您的請求的內容類型設置爲application/x-www-form-urlencoded,或者您可以嘗試下面來自類似問題的解決方案之一。

基於this question,似乎下面的代碼(由菲利普Miosso提供)似乎解決了問題:

// Your app's root module... 
    angular.module('MyModule', [], function($httpProvider) { 
    // Use x-www-form-urlencoded Content-Type 
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; 

    /** 
    * The workhorse; converts an object to x-www-form-urlencoded serialization. 
    * @param {Object} obj 
    * @return {String} 
    */ 
    var param = function(obj) { 
    var query = '', name, value, fullSubName, subName, subValue, innerObj, i; 

    for(name in obj) { 
     value = obj[name]; 

     if(value instanceof Array) { 
     for(i=0; i<value.length; ++i) { 
      subValue = value[i]; 
      fullSubName = name + '[' + i + ']'; 
      innerObj = {}; 
      innerObj[fullSubName] = subValue; 
      query += param(innerObj) + '&'; 
     } 
     } 
     else if(value instanceof Object) { 
     for(subName in value) { 
      subValue = value[subName]; 
      fullSubName = name + '[' + subName + ']'; 
      innerObj = {}; 
      innerObj[fullSubName] = subValue; 
      query += param(innerObj) + '&'; 
     } 
     } 
     else if(value !== undefined && value !== null) 
     query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&'; 
    } 

    return query.length ? query.substr(0, query.length - 1) : query; 
    }; 

    // Override $http service's default transformRequest 
    $httpProvider.defaults.transformRequest = [function(data) { 
    return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data; 
    }]; 
}); 

或者,你也許能夠解決這個問題,通過添加下面的一行代碼到您的PHP:

$params = json_decode(file_get_contents('php://input'),true); 
相關問題