使用jQuery進行ajax請求
$.ajax({ type: 'POST', url:'url.php', data: data, dataType: 'json', success: function(res){ //do something } });
對這個傳輸的數據咱們通常會直接使用serialize()處理後再傳輸,但在發送post請求時jquery會把這個對象轉換爲字符串後再發送,相似"tel=1233&pass=12434"
而AngularJs默認的請求頭是application/json,傳輸的是一個Json數據而不是一個轉換後的字符串,在php端接收的時候不能直接使用$_POST方式接收.這樣是獲取不到數據的.
$POST方式只能接收Content-Type: application/x-www-form-urlencoded提交的數據,也就是表單提交的數據php
解決方案
1.引用JQuery,使用JQuery的$.param()方法序列化參數後傳遞jquery
$http({ method : 'POST', url: 'process.php', data: $.param($scope.formData), //序列化參數 headers: { 'Content-Type': 'application/x-www-form-urlencoded' } ) })
2.使用file_get_contents("php://input")獲取再處理angularjs
$input = file_get_contents("php://input",true); echo $input;