2017-01-16 58 views
0

目前我正在使用下面的代碼,它工作正常。角度:通過表單提交附加數據

$scope.processForm = function($scope.formData) { 
     $http({ 
      method : 'POST', 
      url  : 'process.php', 
      data : $.param($scope.formData), // pass in data as strings 
      headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload) 
     }) 

現在我有一個小的要求。我需要傳遞用戶標識(uid)以及表單數據。 在jquery中它很簡單,但我是新來的角度和沒有太多的經驗。

任何建議如何我可以在Angular中傳遞額外的數據。

感謝

+1

你可以用你的uid參數togheter封裝你FORMDATA在對象expl:'var allData = {'formData':$ scope.formData,'uid':$ scope.uid}',然後將allData對象傳遞給post方法'data:allData' –

+0

formdata爲空。我不知道爲什麼。 – Ironic

+0

試試這個: 1-Remove'headers:{'Content-Type':'application/x-www-form-urlencoded'}' 2 -in your process.php: '$ postContent = file_get_contents(「php: //輸入「); $ req = json_decode($ postContent); $ formData = $ req-> formData; $ uid = $ req-> uid;' –

回答

0

你得到的數據的詳細信息可以將您的formData與您的uid參數一起封裝到對象中:

var allData={'formData': $scope.formData, 'uid': $scope.uid}

然後將allData對象傳遞給您的post方法數據:allData

如果您在獲取process.php(例如未定義數據)時遇到任何問題,您應該讓Angular將內容類型application/json(angularjs的默認行爲)傳遞到您的process.php中,如下所示:

$postContent= file_get_contents("php://input"); 
$req= json_decode($postContent); 
$formData= $req->formData; 
$uid= $req->uid; 

請注意,如果你有一些靜態值第一(「UID」:2),測試將是巨大的,以確保其工作正常

+0

我上面試過,但formData是空的。 $ formData = $ req-> formData;我可以看到uid,但formData是空的。 – Ironic

+0

讓我看看你的表單視圖(角度) –

0

你可以在$ HTTP數據參數發送對象(PHP關聯數組):

$scope.processForm = function($scope.formData) { 
    console.log($scope.formData); 
    $http({ 
     method : 'POST', 
     url  : 'process.php', 
     data : {'data': $scope.formData,'userid':userid}, // no need to use $.param, even never see it in angular 
     headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload) 
    }) 

看到https://scotch.io/tutorials/submitting-ajax-forms-the-angularjs-way關於如何在PHP側

+0

formdata爲空。 – Ironic

+0

我如何訪問php中的數據。郵政不工作。 – Ironic

+0

javascript客戶端中的$ http,它將數據發送到您必須處理它的php頁面。你是如何使用$ scope.processForm()?看到https://scotch.io/tutorials/submitting-ajax-forms-the-angularjs-way – AlainIb